0% found this document useful (0 votes)
26 views13 pages

Report Lab 1 2

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)
26 views13 pages

Report Lab 1 2

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/ 13

National Aviation University

Faculty of Cybersecurity, Computer and Software Engineering


Software engineering department

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

1. Learn the description of class members in the C++ and C# programming


languages.
2. Study the access modifiers of classes and class members in C++ and C#
programming languages.
3. Study the mechanisms of creation, use and destruction of class objects in
programming languages C++ and C#.
4. Write programs in the C++ and C# programming languages that:
• Describe the class in the project, which is a dynamic (C#) or static (C++)
library
• In the case of using input\output operators in the application, mandatory
check the correctness of entered data. If the data was entered incorrectly, the user
must re-enter. For example, if the string should consist only of digits, the user should
receive a message about an incorrect input, if he trying to add letters. And after the
message get the opportunity to re-enter data
• For data members, restrict direct modification using access modifiers. At the
same time, provide for the possibility of reading this data
• The class must have all types of constructors. Show calling of each
constructor when creating objects
• Creating figures/lines and changing them, it is necessary to check the
correctness of the figure or text. For example, specifying the coordinates of a square,
check that the result really is a square, not some other quadrilateral
• In C++, divide the class into .h and .cpp files
• Describe the use of the class in a project that is a console application. In the
console statements can be used to interact with the user input\output
It is allowed
• Using the string data type. At the same time, in the case of variants with
lines, all manipulations with the line should only take place in the class, not outside
of it
• Input-output operations in Main()

Kiev, 2022 Page 2


• Standard casting methods
• Standard methods/functions for performing mathematical operations
It is prohibited
• using standard library methods/functions, except for the lowest level
operations (for example, the function of adding/removing a character in the middle
of a line; at the same time, the calculation square root is allowed)
• templates, collections, generalized collections
• use inheritance
Variant 5

Kiev, 2022 Page 3


C++

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 << ")";
}

Kiev, 2022 Page 4


void Segments::setTheFirstPair(int x1, int y1) {
this->x1 = x1;
this->y1 = y1;
}
void Segments::setTheSecPair(int x2, int 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(int x1, int y1, int x2, int y2) {
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = 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;

Kiev, 2022 Page 5


}
};
Source.cpp
#include <iostream>
#include <string>
#include "Segment.h"
using namespace std;

const int arrLength = 25;


void fillArr(Segments* arr, int length);

int main() {
int x1, y1, x2, y2;

bool inputCorrect = false;

cout << "Enter the coordinates" << endl;

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: ";

Kiev, 2022 Page 6


try
{
string temp;
cin >> temp;
x2 = stoi(temp);
}
catch (...)
{
cout << "Write an integer value"<<endl;
inputCorrect = false;
}
}
inputCorrect = false;
while (!inputCorrect)
{
inputCorrect = true;
cout << "y2: ";
try
{
string temp;
cin >> temp;
y2 = stoi(temp);
}
catch (...)
{
cout << "Write an integer value"<<endl;
inputCorrect = false;
}
}
Segments s(x1, y1, x2, y2);
s.print();
cout << "\nThe length: " << s.length() << endl;
cout << "The angle between the OX axis and the segment: " << s.angle() << endl;
s.setTheFirstPair(4, 3);
s.print();
cout<<"\nThe length: " << s.length()<<endl;
cout<<"The angle between the OX axis and the segment: " << s.angle()<<endl;

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#

Kiev, 2022 Page 7


Segments.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Segments
{
public class Segment
{
private int x1;

private int y1;

private int x2;

private int y2;

public int X1()


{
return this.x1;
}
public int Y1()
{
return this.y1;
}
public int X2()
{

Kiev, 2022 Page 8


return this.x2;
}
public int Y2()
{
return this.y2;
}

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;

Kiev, 2022 Page 9


using System.ComponentModel;
using Segments;

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;

Console.WriteLine("Enter the coordinates");

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)
{

Kiev, 2022 Page 10


Console.WriteLine("Write an integer value");
flag = false;
}
}
flag = false;
while (!flag)
{
flag = true;
Console.WriteLine("y2:");
try
{
y2 = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Write an integer value");
flag = false;
}
}

Segment s = new Segment(x1, y1, x2, y2);


s.print();
Console.WriteLine("The length: " + s.length());
Console.WriteLine("The angle between the OX axis and the segment: " + s.angle());

Segment[] arr = new Segment[arrLength];


fillArr(arr);

}
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

Kiev, 2022 Page 11


program. There are typically three access modifiers: public, private, and protected.

2. How are encapsulation and access modifiers related?


Encapsulation is the practice of hiding implementation details of an object and providing a public
interface through which other objects can interact with it. Access modifiers are a fundamental
tool for achieving encapsulation, as they limit the visibility of members to only those parts of the
program that need to access them.

3. Explain the principles of abstraction and encapsulation, give examples.


Abstraction is the practice of simplifying complex systems by breaking them down into smaller,
more manageable parts. Encapsulation is a technique that supports abstraction by hiding the
details of an object's implementation, allowing the object to be used as a single unit without
worrying about the internal details. For example, a car can be abstracted as an object with
properties such as speed, direction, and fuel level, while encapsulation ensures that these
properties are not accessed or modified in unexpected ways by other parts of the program.

4. How to create a class and an object using the C++ language?


Class:
class MyClass {
private:
int x;
public:
void setX(int n) { x = n; }
int getX() { return x; }
};

Object:
MyClass obj;
obj.setX(10);
int value = obj.getX();

5. How to create a class and an object using the C# language?


Class:
class MyClass {
private int x;
public void SetX(int n) { x = n; }
public int GetX() { return x; }
}
Object:
MyClass obj = new MyClass();
obj.SetX(10);
int value = obj.GetX();

6. What is the difference between classes and structures in C#


In C#, a class is a reference type, and a structure is a value type. Classes and structures are
similar in syntax and capabilities, but they have different performance characteristics and
behaviors. The main differences between classes and structures are:
 Classes are allocated on the heap, while structures are allocated on the stack.
 Classes support inheritance and polymorphism, while structures do not.
 Classes are more flexible than structures, but they are also more memory-intensive and
slower than structures.
 Structures are more lightweight than classes, but they are also more limited in terms of

Kiev, 2022 Page 12


functionality and flexibility.

Kiev, 2022 Page 13

You might also like