Report Lab 2.1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

National Aviation University

Faculty of Cybersecurity, Computer and Software Engineering


Software engineering department

Basics of Programming
Laboratory work 2.1

STUDY OF THE MECHANISM OF SINGLE

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

Kyiv, 2023 Page 2


{
protected string text = "";

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

public void sortString()


{
char temp;
StringBuilder temp2 = new StringBuilder(text);
for (int i = 0; i < StringLength(text); i++)
{

for (int j = 0; j < StringLength(text) - 1; j++)


{
if (temp2[j] > temp2[j + 1])
{
temp = temp2[j];
temp2[j] = temp2[j + 1];
temp2[j + 1] = temp;
}
}

Kyiv, 2023 Page 3


}
text = temp2.ToString();
}
public string value
{
get { return text; }
}
}
}

C++
Source.cpp
#include "Strings.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{

Strings newText = Strings("5130hgfedFGRAcba");


cout << "Length is " << newText.StringLength() << endl;
StringsLettrs Text = StringsLettrs("gfdf6hdj787FGA");
cout << "String: " << Text.ToString() << endl;
Text.sort();
cout << "String: " << Text.ToString() << endl;
return 0;
}
Strings.h
#pragma once
#include <iostream>
#include<string>
using namespace std;

class Strings {
protected:
string text = "";
public:
Strings() {}
Strings(string newText)
{
text = newText;
}
~Strings() {}

static int StringLength(string input) {


int len = 0;
while (input[len] != '\0') {
len++;
}
return len;
}

Kyiv, 2023 Page 4


int StringLength() {
return StringLength(text);
}
};

class StringsLettrs : public Strings {


public:
StringsLettrs(string Text) {
for (int i = 0; i < StringLength(Text); i++) {
if (Text[i] > 64 && Text[i] < 123) {
text += Text[i];
}
}
}
~StringsLettrs(){}

void sort()
{
char temp;
for (int i = 0; i < StringLength(text); i++) {

for (int j = 0; j < StringLength(text) - 1; j++) {


if (text[j] > text[j + 1]) {
temp = text[j];
text[j] = text[j + 1];
text[j + 1] = temp;
}
}
}
}
string ToString() {
return text;
}

};

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.

2. What are the types of inheritance?


Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e.
one subclass is inherited by one base class only.
Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from
more than one class.
Multilevel Inheritance: In this type of inheritance, a derived class is created from another
derived class.

Kyiv, 2023 Page 5


Hierarchical Inheritance: In this type of inheritance, more than one subclass is inherited from a
single base class.
Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than one
type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.
Multipath inheritance: A derived class with two base classes and these two base classes have
one common base class is called multipath inheritance. Ambiguity can arise in this type of
inheritance.

3. Explain the role of the access specifier in inheritance.


Access specifiers are used to control the visibility and accessibility of class members (variables
and methods) from other parts of the program. In inheritance, access specifiers play an important
role in determining the level of access that derived classes have to the members of the base class.

4. Explain the essence of single inheritance, give an example.


A derived class inherits properties and behaviors from a single base class. This allows for code
reusability and the creation of a hierarchy of related classes.
class A
{
public:
int a;
A()
{
cout << "A class" << endl;
}
};
class B : public A
{
public:
int b;
B()
{
cout << "B class" << endl;
}
};

5. Explain the essence of multiple inheritance, give an example.


A derived class inherits properties and behaviors from multiple base classes.
class B
{
};
class C
{
};
class A : public B, public C
{
};

6. Explain why there is no multiple inheritance in C#.


C# does not support multiple inheritance because it can lead to issues with ambiguity and
complexity in the inheritance hierarchy. Instead, C# supports interface inheritance, where a class
can implement multiple interfaces that define a set of behaviors without introducing ambiguity in
the inheritance hierarchy. This allows for greater flexibility and code reusability while
maintaining a simpler and more manageable inheritance hierarchy.

Kyiv, 2023 Page 6


7. Explain the difference between a direct base class and an indirect base class.
In this example, Figure is the indirect base class of Square, since it is inherited by Rectangle,
which is then inherited by Square. Rectangle is the direct base class of Square, since it is directly
inherited by Square.
class Figures
{
};
class Rectangle : public Figures
{
};
class Square : public Rectangle
{
};

8. Explain the essence of inheritance based on indirect virtual base classes.


Inheritance based on indirect virtual base classes allows for the creation of a shared base class
that is inherited by multiple derived classes, ensuring that each derived class only contains one
instance of the shared base class.

9. How are constructors of base and derived classes called?


When a derived class is instantiated, the constructor of the base class is called first to initialize
the inherited members, followed by the constructor of the derived class to initialize its own
members. This ensures that all members are properly initialized before the object is used.

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:

Kyiv, 2023 Page 7


bool engineOn;
};
class Car : public Vehicle {
private:
bool engineOn;
};

Kyiv, 2023 Page 8

You might also like