You get a bonus - 1 coin for daily activity. Now you have 1 coin

2.4. Abstract classes

Lecture



An abstract class is a class in which there is at least one pure (empty) virtual function. A pure virtual function is a component function that has the following definition: virtual type function_name (formal_parameter_list) = 0; A pure virtual function does not do anything and is not available for calls. Its purpose is to serve as a basis for replacing its functions in derived classes. The abstract class can only be used as a base for derived classes. The abstract classes mechanism is designed to represent general concepts that are supposed to be concretized in the future. In this case, the construction of the class hierarchy is performed as follows. At the head of the hierarchy is the abstract base class. It is used to inherit the interface. Derived classes will concretize and implement this interface. In the abstract class, pure virtual functions are declared, which essentially are abstract methods .

Example 2.4.1

Example 2.4.1

class Base {

public:

Base (); // default constructor

Base (const Base &); // copy constructor

virtual ~ Base (); // virtual destructor

virtual void Show () = 0; // pure virtual function

// other pure virtual functions

protected:

// protected class members

private:

// often remains empty, otherwise it will interfere with future developments

};

class Derived: virtual public Base {

public:

Derived (); // default constructor

Derived (const Derived &); // copy constructor

Derived (parameters); // constructor with parameters

virtual ~ Derived (); // virtual destructor

void Show (); // redefined virtual function

// other overridden virtual functions

Derived & operator = (const Derived &); // overloaded assignment operation

// its meaning will be clear after reading chapter 3

// other overloaded operations

protected:

// is used instead of private if inheritance is expected

private:

// used for implementation details

};

Compared to regular classes, abstract classes enjoy “limited rights”. Namely:

· It is impossible to create an abstract class object;

· An abstract class cannot be used to specify the type of a parameter of a function or the type of a return value of a function;

· Abstract class cannot be used in explicit type conversion; pointers and references to an abstract class can be defined at the same time.

An abstract class object cannot be a formal parameter of a function, but a pointer to an abstract class can be a formal parameter. In this case, it is possible to pass the value of the pointer to the derived object as the actual parameter to the called function, replacing it with the pointer to the abstract base class. Thus we get polymorphic objects .

Example 2.4.2

Example 2.4.2

We form a simply linked list containing objects of different classes derived from the same abstract class.

#include

#include

// Abstract class

class Person

{

public:

Person ()

{strcpy (name, "NONAME"); age = 0; next = 0;};

Person (char * NAME, int AGE)

{strcpy (name, NAME); age = AGE; next = 0;}

virtual ~ Person () {};

virtual void Show () = 0;

virtual void Input () = 0;

friend class List; // so that next is available in the List class

protected:

char name [20]; //name

int age; //age

Person * next; // pointer to the next object in the list

};

// Derived class- STUDENT

class Student: public Person {

public:

Student ()

{grade = 0;}

Student (char * NAME, int AGE, float GRADE): Person (NAME, AGE)

{grade = GRADE; }

void Show () {cout << "name =" << name << "age =" << age << "grade =" << grade << endl;}

void Input ()

{cout << "name ="; cin >> name;

cout << "age ="; cin >> age;

cout << "grade ="; cin >> grade;}

protected:

float grade; // rating

};

// Derived class- Teacher

class Teacher: public Person {

public:

Teacher ()

{work = 0;}

Teacher (char * NAME, int AGE, int WORK): Person (NAME, AGE)

{work = WORK;}

void Show () {cout << "name =" << name << "age =" << age << "work =" << work << endl;}

void Input ()

{

cout << "name ="; cin >> name;

cout << "age ="; cin >> age;

cout << "work ="; cin >> work;}

protected:

int work; // experience

};

// LIST class

class List

{

private:

Person * begin;

public:

List () {begin = 0;}

~ List ();

void Insert (Person *);

void Show ();

};

List :: ~ List ()

{Person * r;

while (begin! = 0) {r = begin; begin = begin-> next; delete r;}

}

void List :: Insert (Person * p) {

Person * r;

r = begin; begin = p; p-> next = r;}

void List :: Show ()

{Person * r;

r = begin;

while (r! = 0)

{r-> Show (); r = r-> next;}

}

void main ()

{

List list;

Student * ps;

Teacher * pt;

ps = new Student ("Ivanov", 21.50.50);

list.Insert (ps);

pt = new Teacher ("Kotov", 34,10);

list.Insert (pt);

ps = new Student;

ps-> Input ();

list.Insert (ps);

pt = new Teacher;

pt-> Input ();

list.Insert (pt);

list.Show ();

};

created: 2015-12-20
updated: 2021-03-13
132407



Rating 8 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

C ++ (C plus plus)

Terms: C ++ (C plus plus)