Report Lab 2.1
Report Lab 2.1
Report Lab 2.1
Basics of Programming
Laboratory work 2.1
INHERITANCE OF CLASSES
Variant - 5
Prepared by:
student of SE-124 en.,
Demchuk Oksana
Accepted by:
Vasilieva M.D.
Kyiv, 2023
Task
1. To study the mechanism of single inheritance of classes in C++ and C#
programming languages.
2. Write programs in the C++ and C# languages that demonstrate the use of the
mechanism of single inheritance of classes according to the variant.
Procedure
C#
Program.cs
using Labor_w_21;
namespace MainProgram
{
class Program
{
static void Main(string[] args)
{
StringClass test = new StringClass("HAYs456haygdF2Ag");
Console.WriteLine("Length is " + test.StringLength());
StringLettrs example = new StringLettrs("HAYs456haygdF2Ag");
Console.WriteLine("Length is " + StringLettrs.StringLength(example.value));
Console.WriteLine("String: " + example.value);
example.sortString();
Console.WriteLine("String: " + example.value);
}
}
StringLettrs.cs
using System.Text;
namespace Labor_w_21
{
class StringClass
public StringClass() { }
public StringClass(string newText)
{
text = newText;
}
public static int StringLength(string input)
{
int len = 0;
foreach (char c in input)
{
len++;
}
return len;
}
public int StringLength()
{
return StringLength(text);
}
~StringClass()
{
}
class StringLettrs : StringClass
{
public StringLettrs(string newText)
{
for (int i = 0; i < StringLength(newText); i++)
{
if (newText[i] > 64 && newText[i] < 123)
{
text += newText[i];
}
}
}
~StringLettrs() { }
C++
Source.cpp
#include "Strings.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
class Strings {
protected:
string text = "";
public:
Strings() {}
Strings(string newText)
{
text = newText;
}
~Strings() {}
void sort()
{
char temp;
for (int i = 0; i < StringLength(text); i++) {
};
Self-checking questions
1. What is the essence of the inheritance mechanism?
Inheritance is one of the key features of Object-oriented programming in C++. It allows us to
create a new class (derived class) from an existing class (base class).
The derived class inherits the features from the base class and can have additional features of its
own.
10. Explain how to control the calling of base class constructors in the derived class
constructor, give an example.
To control the calling of the base class constructors in the derived class constructor, the derived
class constructor can use an initializer list to explicitly call the desired base class constructor.
class Figures
{
public:
Figures() {};
};
class Rectangle : virtual public Figures
{
public:
Rectangle():Figures(){};
};
class Square : virtual public Rectangle
{
public: Square();
};
Square::Square() : Rectangle(), Figures() {
// Derived class constructor code
}
11. How and for what purpose to hide a member of the base class? Give an example of
concealment.
The purpose of hiding a member of the base class is to restrict its visibility and prevent direct
access by derived classes or external code, allowing for better control over the internal state of
the object.
class Vehicle {
public:
int speed;
private: