Report Lab1.3
Report Lab1.3
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"
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;
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() {
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;
}
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()
{}
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);
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.