0% found this document useful (0 votes)
9 views9 pages

Report Lab1.3

Uploaded by

dea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Report Lab1.3

Uploaded by

dea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

National Aviation University

Faculty of Cybersecurity, Computer and Software Engineering


Software engineering department

Basics of Programming
Laboratory work 1.3
STUDY OF THE INTERNAL STRUCTURE OF CLASSES
Variant - 5

Prepared by:
student of SE-124B,
Demchuk Oksana

Accepted by:
Vasilieva M.D.

Kiev, 2024
Tasks
1. Write the C ++ code by taking a class from practice 1.1. Overload operators according to the variants.
2. Write the C# code by taking a class from practice 1.1. Overload operators according to the variants.
3. Output the initial values of the objects and the result of execution of operators overload into the console

C++

Header.h
#pragma once
#include <iostream>
#include "Math.h"

Kiev, 2024 Page 2


using namespace std;

class Segments {
private:
double x1, y1, x2, y2;
public:
Segments();
Segments(double, double, double, double);
Segments(Segments*);
~Segments();
void print();
void setTheFirstPair(double, double);
void setTheSecPair(double, double);
double length();
double angle();
void SetValues(double, double, double, double);
int X1() {
return this->x1;
}
int Y1() {
return this->y1;
}
int X2() {
return this->x2;
}
int Y2() {
return this->y2;
}
Segments operator -(double a) {
return new Segments(x1 - a, y1 - a, x2 - a, y2 - a);

}
friend Segments operator -(double, Segments&);
/*Segments operator -(Segments& second) {
double k = second.length() / length();
return new Segments(x1, y1, x2 - (x2 - x1) * k, y2 - (y2 - y1) * k);
}*/
friend Segments operator -(Segments&, Segments&);
friend bool operator >(Segments&, Segments&);
Segments operator ++(int) {
return new Segments(x1++, y1++, x2++, y2++);
}
operator bool() const {
return x1!=0&&y1!=0&&x2!=0&&y2!=0;
}
};

Source.cpp
#include <iostream>
#include "Header.h"
using namespace std;
Segments::Segments() {
x1 = 0;
y1 = 0;
x2 = 0;

Kiev, 2024 Page 3


y2 = 0;
}
Segments::Segments(double x1, double y1, double x2, double y2) {
SetValues(x1, y1, x2, y2);
}
Segments::Segments(Segments* segment) {
x1 = segment->x1;
y1 = segment->y1;
x2 = segment->x2;
y2 = segment->y2;
}
Segments::~Segments() {}

void Segments::print() {
cout << "\nThe coordinates: ";
cout << "(" << x1 << ";" << y1 << "),(" << x2 << "; " << y2 << ")";
}
void Segments::setTheFirstPair(double x1, double y1) {
this->x1 = x1;
this->y1 = y1;
}
void Segments::setTheSecPair(double x2, double y2) {
this->x2 = x2;
this->y2 = y2;
}
double Segments::length() {
double a = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
return a;
}
double Segments::angle() {
if ((x1 - x2) == 0) {
return 90;
}
else {
double a = abs(y1 - y2) / abs(x1 - x2);
return(180 / 3.14) * atan(a);
}
}
void Segments::SetValues(double x1, double y1, double x2, double y2) {
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = y2;
}
Segments operator -(double a, Segments& input) {
return input - a;
}
Segments operator -(Segments& first, Segments& second) {
double k = second.length() / first.length();
return new Segments(first.x1, first.y1, first.x2 - (first.x2 - first.x1) * k, first.y2 - (first.y2 - first.y1) * k);
}
bool operator >(Segments& first,Segments& second) {
return first.length() > second.length();
}

int main() {

Segments test = new Segments(0, 8, 6, 0);


Segments test2 = new Segments(3, 1, 5, 7);

Kiev, 2024 Page 4


cout << (test2 - test).length()<<endl;
cout << (test - 1.1).length() << endl;
cout << (test).length()<<endl;
cout << (test > test2)<<endl;
cout << (test++).length() << endl;
cout << (test ? "true" : "false") << endl;
}

C#

Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS
{
public class Triangles
{
private double x1, x2, x3, y1, y2, y3;
public Triangles(double x1, double y1, double x2, double y2, double x3, double y3)

{
this.x1 = x1;
this.y1 = y1;

Kiev, 2024 Page 5


this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;

}
public Triangles(Triangles obj)
{
this.x1 = obj.x1;
this.y1 = obj.y1;
this.x2 = obj.x2;
this.y2 = obj.y2;
this.x3 = obj.x3;
this.y3 = obj.y3;
}
public static double sideLength(double x1, double y1, double x2, double y2)
{
return (double)Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
}
public double perimeter()
{
return sideLength(x1, y1, x2, y2) + sideLength(x2, y2, x3, y3) + sideLength(x1, y1, x3, y3);
}
public double area()
{
double semiperimeter = this.perimeter() / 2;
return Math.Sqrt(semiperimeter * (semiperimeter - sideLength(x1, y1, x2, y2)) * (semiperimeter - sideLength(x2,
y2, x3, y3)) * (semiperimeter - sideLength(x1, y1, x3, y3)));
}
public void changing(double x1, double y1, double x2, double y2, double x3, double y3)
{
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
}

bool checking()
{
bool result = true;

if (sideLength(x1, y1, x2, y2) > 0 && sideLength(x2, y2, x3, y3) > 0 && sideLength(x1, y1, x3, y3) > 0)
{
Console.WriteLine("The length of all sides are greater than zero.");
result = true;
}

return result;
}
public void printInfo()
{
Console.WriteLine("A("+x1+";"+ y1+")"+" B(" +x2 +";" + y2 +")"+" C("+ x3+";"+ y3+")");
Console.WriteLine("The perimeter: " + this.perimeter());
Console.WriteLine("The area: " + this.area());
}
~Triangles()
{}

Kiev, 2024 Page 6


public static Triangles operator *(Triangles arg1, double arg2)
{
return new Triangles(arg1.x1 * arg2, arg1.y1 * arg2, arg1.x2 * arg2, arg1.y2 * arg2, arg1.x3 * arg2, arg1.y3 *
arg2);
}
public static Triangles operator *( double arg2, Triangles arg1)
{
return arg1 * arg2;
}
public static Triangles operator +(Triangles arg1, Triangles arg2)
{
return new Triangles(arg1.x1 + arg2.x1, arg1.y1 + arg2.y1, arg1.x2 + arg2.x2, arg1.y2 + arg2.y2, arg1.x3 +
arg2.x3, arg1.y3 + arg2.y3);
}
public static bool operator <(Triangles first,Triangles second)
{
return first.area() < second.area();
}
public static bool operator >(Triangles first, Triangles second)
{
return first.area() > second.area();
}
public static Triangles operator ++(Triangles arg1)
{
return new Triangles(arg1.x1+1, arg1.y1+1, arg1.x2+1, arg1.y2+1, arg1.x3+1, arg1.y3+1);
}
public static bool operator |(Triangles t1, Triangles t2)
{
return t1.x1 != 0 && t1.y1 != 0 && t1.x2 != 0 && t1.y2 != 0 && t1.x3 != 0 && t1.y3 != 0 &&
t2.x1 != 0 && t2.y1 != 0 && t2.x2 != 0 && t2.y2 != 0 && t2.x3 != 0 && t2.y3 != 0;
}
}
}

Program.cs
namespace CS
{
class Program
{
static void Main(string[] args)
{
Triangles test = new Triangles(0, 0, 0, 4, 3, 0);
Triangles test2 = new Triangles(0, 0, 0, 5, 12, 0);
Console.WriteLine("----------");
test.printInfo();
Console.WriteLine("----------");
(test * 3).printInfo();
Console.WriteLine("----------");
(3 * test).printInfo();
Console.WriteLine("----------");
(test + test2).printInfo();
Console.WriteLine("----------");
Console.WriteLine(test < test2);
Console.WriteLine("----------");
(test++).printInfo();
Console.WriteLine("----------");
Console.WriteLine(test | test2);

Kiev, 2024 Page 7


}

Conclusion
We studied overloading of operators to simplify operations like + and * between data from
classes.
Self-checking questions
1. What is the purpose to overload operators?
The purpose of operator overloading is to provide a way to define the behavior of operators when
applied to user-defined types. By overloading operators, we can make our code more expressive
and readable, and enable operations on our custom data types that behave like built-in types.

2. What is the difference in the overload of unary and binary operators overloading as C++
?
The main difference between unary and binary operator overloading in C++ is the number of
operands that they take. Unary operators take a single operand, while binary operators take two
operands.

3. What is the difference between unary and binary operators as friendly functions
overloading in C ++?
The difference between unary and binary operators as friend functions overloading in C++ is that
unary operators as friend functions take no arguments (or one “int” if it is postfix), while binary
operators as friend functions take two arguments.

4. What is the difference between binary operators overloading as class members and
friendly functions in C ++?
The main difference between binary operators overloading as class members and friendly
functions in C++ is the way they are invoked. When overloading binary operators as class
members, the left operand is an instance of the class, and the right operand is passed as an
argument. When overloading binary operators as friend functions, both operands are passed as
arguments.

6. What is the difference between the overload of unary operators in C++ and C#?
In C++, unary operators can be overloaded as member functions or as friend functions, while in
C#, they can only be overloaded as static methods.

7. What is the difference between binary operators in C++ and C#?


In C++, binary operators can be overloaded as member functions or as friend functions, while in
C#, they can only be overloaded as static methods.

8. What is the feature of overloading True/False operators in C#?


In C#, the True/False operators (also known as the boolean operators) can be overloaded to
provide a way to define the logical behavior of an object. By overloading these operators, we can

Kiev, 2024 Page 8


enable our custom types to be used in boolean expressions.

9. What are the restrictions on Operators overload in C++?


Scope resolution operator ::, Pointer to member variable *, Conditional operator ? :, Sizeof
operator sizeof()

10.What are the restrictions on the overload of operators in C#?


=, . , ?:, ->, new, is, as, sizeof

Kiev, 2024 Page 9

You might also like