Report Lab 1 2
Report Lab 1 2
Basics of Programming
Laboratory work 1.2
STUDY OF THE INTERNAL STRUCTURE OF CLASSES
Variant 5
Prepared by:
student of SE-121 en.,
Demchuk Oksana
Accepted by:
Vasilieva M.D.
Kiev, 2022
Tasks
SegmentsLib.cpp
#include "pch.h"
#include "Segment.h"
#include <iostream>
#include "Math.h"
using namespace std;
Segments::Segments() {
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
}
Segments::Segments(int x1, int y1, int x2, int 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 << ")";
}
Segment.h
#pragma once
class Segments {
private:
int x1, y1, x2, y2;
public:
Segments();
Segments(int, int, int, int);
Segments(Segments&);
~Segments();
void print();
void setTheFirstPair(int,int);
void setTheSecPair(int,int);
double length();
double angle();
void SetValues(int, int, int, int);
int X1() {
return this->x1;
}
int Y1() {
return this->y1;
}
int X2() {
return this->x2;
}
int Y2() {
return this->y2;
int main() {
int x1, y1, x2, y2;
while (!inputCorrect)
{
inputCorrect = true;
cout<<"x1: ";
try
{
string temp;
cin >> temp;
x1 = stoi(temp);
}
catch (...)
{
cout<<"Write an integer value"<<endl;
inputCorrect = false;
}
}
inputCorrect = false;
while (!inputCorrect)
{
inputCorrect = true;
cout << "y1: ";
try
{
string temp;
cin >> temp;
y1 = stoi(temp);
}
catch (...)
{
cout << "Write an integer value"<<endl;
inputCorrect = false;
}
}
inputCorrect = false;
while (!inputCorrect)
{
inputCorrect = true;
cout << "x2: ";
Segments arr[arrLength];
fillArr(arr, arrLength);
return 0;
}
void fillArr(Segments* arr, int length) {
for (int iter = 0; iter < length; iter++)
{
cout << "Iteration: " << iter << endl;
arr[iter].SetValues(iter < length / 2 ? 0 : arr[iter - length / 2].X1(),
iter < length / 2 ? iter + 2 : arr[iter - length / 2].Y1(),
iter < length / 2 ? iter + 1 : arr[iter - length / 2].X2(),
iter < length / 2 ? 1 : arr[iter - length / 2].Y2());
cout << "Value: " << arr[iter].length() << endl;
}
}
C#
namespace Segments
{
public class Segment
{
private int x1;
public Segment()
{
x1 = 0;y1 = 0;x2 = 0;y2 = 0;
}
public Segment(int x1, int y1, int x2, int y2)
{
SetValues(x1, y1, x2, y2);
}
public Segment(Segment obj)
{
x1 = obj.x1;
y1 = obj.y1;
x2 = obj.x2;
y2 = obj.y2;
}
public void print()
{
Console.WriteLine("\nThe coordinates:");
Console.WriteLine("(" + x1 + "; " + y1 + "),(" + x2 + "; " + y2 + ")");
}
public double length()
{
double a = Math.Sqrt(Math.Pow((x1-x2),2) + Math.Pow((y1-y2), 2));
return a;
}
public double angle()
{
if ((x1-x2) == 0) return 90;
else
{
double a = Math.Abs(y1-y2)/Math.Abs(x1-x2);
return (180 / Math.PI) * Math.Atan(a);
}
}
public void SetValues(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
}
}
Program.cs
using System;
namespace Laboratory2
{
class Program
{
const int arrLength = 25;
static void Main(string[] args)
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
bool flag = false;
while (!flag)
{
flag = true;
Console.WriteLine("x1:");
try
{
x1 = Convert.ToInt32(Console.ReadLine());
}
catch(Exception)
{
Console.WriteLine("Write an integer value");
flag = false;
}
}
flag = false;
while (!flag)
{
flag = true;
Console.WriteLine("y1:");
try
{
y1 = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Write an integer value");
flag = false;
}
}
flag = false;
while (!flag)
{
flag = true;
Console.WriteLine("x2:");
try
{
x2 = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
}
static void fillArr(Segment[] arr)
{
for (int iter = 0; iter < arrLength; iter++)
{
Console.WriteLine("Iteration: " + iter );
arr[iter]=new Segment(iter < arrLength / 2 ? 0 : arr[iter - arrLength / 2].X1(),
iter < arrLength / 2 ? iter + 2 : arr[iter - arrLength / 2].Y1(),
iter < arrLength / 2 ? iter + 1 : arr[iter - arrLength / 2].X2(),
iter < arrLength / 2 ? 1 : arr[iter - arrLength / 2].Y2());
Console.WriteLine("Value: " + arr[iter].length());
}
}
}
}
Conclusion
We studied c++ and c# classes, created static and dynamic libraries, and connected them to
projects instead of Header.h files
Self-checking questions
1. Describe the access modifiers.
Access modifiers are keywords in object-oriented programming languages that determine the
accessibility of class members (variables, methods, constructors) from other parts of the
Object:
MyClass obj;
obj.setX(10);
int value = obj.getX();