0% found this document useful (0 votes)
2 views

Unit 3 Inheritance

The document explains abstract classes and virtual functions in C++, highlighting their roles in object-oriented programming. It details the characteristics of abstract classes, including the necessity of pure virtual functions, and discusses the concept of virtual functions for achieving runtime polymorphism. Additionally, it covers pointers, wild pointers, the 'this' pointer, and the concept of polymorphism, providing examples and syntax for better understanding.

Uploaded by

rohini
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 3 Inheritance

The document explains abstract classes and virtual functions in C++, highlighting their roles in object-oriented programming. It details the characteristics of abstract classes, including the necessity of pure virtual functions, and discusses the concept of virtual functions for achieving runtime polymorphism. Additionally, it covers pointers, wild pointers, the 'this' pointer, and the concept of polymorphism, providing examples and syntax for better understanding.

Uploaded by

rohini
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT 3

ABSTRACT CLASSES

 An abstract class is a class not used for creating objects. It is designed only to act as a base class.
These classes are similar to a skeleton on which new classes are designed. These classes contain pure
virtual functions.
 A pure virtual function (or abstract function) in C++ is a virtual function for which we can have an
implementation, But we must override that function in the derived class, otherwise, the derived class
will also become an abstract class. A pure virtual function is declared by assigning 0 in the
declaration.
 abstract class must include at least one pure virtual function, it may also have fully defined member
functions.
Syntax:
class classname //abstract class
{
//data members
public:
//pure virtual function
/* Other members */
};
Virtual Function
A virtual function (also known as virtual methods) is a member function that is declared within a base class
and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a
reference to the base class, you can call a virtual function for that object and execute the derived class’s
version of the method.

• Virtual functions ensure that the correct function is called for an object, regardless of the type of reference
(or pointer) used for the function call.
• They are mainly used to achieve Run time polymorphism.
• Functions are declared with a virtual keyword in a base class.
• The resolving of a function call is done at runtime.
Rules for Virtual Functions
The rules for the virtual functions in C++ are as follows:

• Virtual functions cannot be static.


• A virtual function can be a friend function of another class.
• Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime
polymorphism.
• The prototype of virtual functions should be the same in the base as well as the derived class.
• They are always defined in the base class and overridden in a derived class. It is not mandatory for the derived
class to override (or re-define the virtual function), in that case, the base class version of the function is used.
Example
class Triangle: public Shape
#include <iostream>
{
class Shape
public: int getArea()
{
{
protected: int width, height;
return (width * height)/2;
public: virtual int getArea() = 0;
}
void setWidth(int w)
};
{
int main()
width = w;
{
}
Rectangle r;
void setHeight(int h)
Triangle t;
{
r.setWidth(5);
height = h;
r.setHeight(7);
}
cout<< "\nRectangle area: " <<r.getArea() <<endl;
};
t.setWidth(5);
class Rectangle: public Shape
t.setHeight(7);
{
cout<< "\nTriangle area: " <<t.getArea() <<endl;
public: int getArea() return 0;
{ }
return (width * height);
} Output: Rectangle area: 35
}; Triangle area: 17
Pointers: void pointer
• Pointers can also be declared as void type. void pointers cannot be dereferenced without
explicit-type conversion. This is because, being void, the compiler cannot determine the size
of the object that the pointer points to.
• Though void pointer declaration is possible, void variable declaration is not allowed because
memory is not allocated to them, and there is no place to store the address
• Thus, the declaration void p will display an error message “Size of ‘p’ is unknown or zero”
after compilation..
• A void pointer can point to any type of variable with proper type casting.
• The size of the void pointer displayed will be two. When a pointer is declared as void, two
bytes are allocated to it. Later, using type casting, the number of bytes can be allocated or de-
allocated.
Program to declare a void pointer. Assign address of int, float, and char variables to the void
pointer using typecasting method. Display the contents of various variables.

#include<iostream.h>
using namespace std;  In the above example, variables p, d, and c are variables of
int p;
type int, float, and char, respectively.
float d;  Pointer pt is a pointer of type void. These entire variables are
char c;
declared before main(). The pointer is initialized with the
void *pt = &p; // pt points to p
address of integer variable p.
int main ()  The statement *(int *) pt = 12 assigns the integer value 12 to
{
pointer pt, that is, to a variable p.
*(int *) pt = 12;  The contents of the variable p are displayed using the
cout<<“\n p=”<<p;
succeeding statement.
pt = &d; // pt points to d  The declaration *(int *) tells the compiler that the value
*(float *)pt = 5.4;
assigned is of integer type.
cout<<“\n r=”<<d;  Thus, the assignment of float and char type is carried out.
pt=&c; // pt points to c  The statements *(int *) pt = 12, *(float *) pt = 5.4, and
*(char* )pt=‘S’;
*(char*) pt = ‘S’ help the compiler exactly determine the
cout<<“\n c=”<<c;
size of data type.
}

Output p=12 r=5.4 c=S


wild POINTERS
• When a pointer points to an unallocated memory location or to a data value whose memory is de-allocated,
such a pointer is called a wild pointer.
• The wild pointer generates garbage memory location and pendent reference.
• When a pointer pointing to a memory location vanishes, the memory is transformed into garbage memory. It
indicates that a memory location exists but a pointer is destroyed.
• This happens when the memory is not de-allocated explicitly.
• The pointer becomes a wild pointer due to the following reasons:
(1) Pointer declared but not initialized
(2) Pointer alteration
(3) Accessing destroyed data
• When a pointer is declared and not initialized, it holds an illicit address. It is very hard to manipulate such a
pointer
Program to use wild pointer.
#include<iostream> Output:
using namespace std; 1 0 1496693867 32766 0 0 1496693885 32766
int main() 1496693930 32766
{
int *x; In this program, pointer x is declared and not initialized. Using
for (int k=0;k<10;k++) for loop, the location-containing pointer is increased, and
cout<<x[k]<<“ “; successive addresses are displayed.
return 0;
}
THE this POINTER
In general the objects look at functions and data members of a class as
• Each object gets its own copy of the data member.
• All-access the same function definition as present in the code segment.
That is each object gets its own copy of data members and all objects share a single copy of
member functions.
Then now question is that if only one copy of each member function exists and is used by
multiple objects, how are the proper data members are accessed and updated?
The compiler supplies an implicit pointer along with the names of the functions as ‘this’.
The ‘this’ pointer is passed as a hidden argument to all non-static member function calls and is
available as a local variable within the body of all non-static functions. ‘this’ pointer is not
available in static member functions as static member functions can be called without any object
(with class name).
program to enter name and age of two persons. Find the elder person. Use this pointer
name display( name x)
{
#include<iostream>
cout<<“\n\n Contents of object n1 (this pointer)”;
using namespace std;
cout<<“\n Name:” << this->str;
class name
cout<<“\n Age:” << this->age;
{
cout<<“\n\n Contents of object n2 (x)”;
char str[15];
cout<<“\n Name: ” << x.str;
int age;
cout<<“\n Age: ” << x.age;
public:
if (this->age > x.age)
void input()
return *this; OUTPUT
{
else Enter Name and age : Mahesh 25
cout<<“\n Enter Name and age : ”;
return x; Enter Name and age : Suresh 30
cin>>str;
} Contents of object n1(this pointer)
cin>>age;
}; Name : Mahesh
}
int main() Age : 25
void show()
{ Contents of object n2 (x)
{
name n,n1,n2; Name : Suresh
cout<<“\n\nElder person ”;
n1.input(); Age : 30
cout<<“\n Name: ”<<str;
n2.input(); Elder person
cout<<“\n Age: ”<<age;
n=n1.display(n2); Name : Suresh
}
n.show(); Age : 30
}
number min( number t)

cout<<t.num<<"\n"<<num;

if (t.num<num)
#include<iostream>
using namespace std; return t;

class number else


{ return *this; }
int num; };
public :
int main()
void input()
{ {

cout<< "\n Enter a Number :"; number n,n1,n2;


cin>>num; n1.input();
} n2.input();
void show()
n=n1.min(n2);
{ cout<<"\n The Minimum Number :
"<<num; n.show();

} }

Enter a Number :152

Enter a Number :458

458 152

The Minimum Number : 152


Polymorphism

One of the key features offered by C++ is polymorphisms.


It is essential to know the concepts of polymorphisms and associated
topics.
In this chapter, we are going to learn binding, polymorphisms, and
virtual functions.
The word poly means many, and morphism means several forms.
Both the words are derived from Greek language Thus, by combining
these two words, a new whole word called polymorphism is created,
which means various forms.
We have learnt about overloading of functions and operators.
It is also one type of polymorphism. The information pertaining to
various overloaded member functions and arguments is known to the
compiler while compiling.

You might also like