0% found this document useful (0 votes)
35 views2 pages

Virtual Functions

The document defines an abstract class and virtual method in C++. It then provides an example program that declares a superclass called Student with a virtual read method. It declares two subclasses, UndergraduateStudent and PostgraduateStudent that inherit from Student and override the read method. The main function instantiates objects of the three classes and demonstrates inheritance by calling the read method on each object.

Uploaded by

Erick Agutu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Virtual Functions

The document defines an abstract class and virtual method in C++. It then provides an example program that declares a superclass called Student with a virtual read method. It declares two subclasses, UndergraduateStudent and PostgraduateStudent that inherit from Student and override the read method. The main function instantiates objects of the three classes and demonstrates inheritance by calling the read method on each object.

Uploaded by

Erick Agutu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

GATERI CAROLINE NJERI

BIT-008-0043/2009
BIT 2203 ASSIGNMENT III

19th Feb 2010

A: define

(i)Abstract class

In C++, a class is referred to as abstract if it serves (only) as a base or parent for other
classes. That is, you cannot declare a variable of that class nor can you use it as you
would a regular class. To create an abstract class in C++, at least one of its methods must
be pure virtual. A pure virtual method is one that is not implemented or defined in that
class. It is created using = 0; on its right side when declaring it.

(ii)Virtual method

This is a function or method whose behavior can be overridden within an inheriting class
by a function with the same signature.

B: Using C++ programs language declare a super class called student


and let it have at least one virtual method. Also declare two other
classes, i.e. undergraduate student and postgraduate student which
inherit from class student-instantiate objects of both classes and
demonstrate inheritance in implementing your program.
#include <iostream>
#include <vector>

class student
{
public:
virtual void read() const { std::cout << "I like reading." << std::endl;
}
virtual ~ student() {}
};

class undergraduate : public student


{
public:
void read() const { std::cout << "undergraduate Student!" << std::endl;
}
};

class postgraduate: public student


{
public:
void read() const { std::cout << "postgraduate Student!" << std::endl; }
};

};

class Otherstudent : public student


{
};

int main()
{
std::vector< student*> student;
student.push_back( new student() );
student.push_back( new undergraduate () );
student.push_back( new postgraduate () );
student.push_back( new Otherstudent() );

for( std::vector< student*>::const_iterator it = student.begin();


it != student.end(); ++it)
{
(*it)->read();
delete *it;
}

return 0;
}

You might also like