0% found this document useful (0 votes)
273 views44 pages

GCUF BSSE 2nd

It is a practical notebook ? of object oriented programming(oop) for GCUF BSSE or BSCS 2nd Semester.Its course code is CSI-302

Uploaded by

Abubakar Siddiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
273 views44 pages

GCUF BSSE 2nd

It is a practical notebook ? of object oriented programming(oop) for GCUF BSSE or BSCS 2nd Semester.Its course code is CSI-302

Uploaded by

Abubakar Siddiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

1

Practical Notebook
Prepared By:
Student Name: ali

Roll No: 123

Subject
Programming Fundamentals CSI-301 4(3-1)

DEPARTMENT OF SOFTWARE ENGINEERING


DIVISIONAL PUBLIC SCHOOL AND COLLEGE FAISALABAD

2023
2

Certificate

This is to certify that ali bearing Registration No. 2022-GCUF-07123


is a hardworking student at DPSC and has completed the Practical
Notebook of subject Programming Fundamentals Subject Code CSI-
301 at the Department of Software Engineering, Divisional Public
School and College Faisalabad is an authentic work and carried out
under my supervision and guidance.

Supervisor
Saeed Rasheed Signature

Head of Department Signature


3

Acknowledgement
I carried the work reported in this Practical Notebook under the
supervision of the Supervisor Mr. Talha Rasheed, at Divisional Public
School and College Faisalabad. I as a result of this declare that this
notebook and the contents of the practical notebook are the product of
my practical implementation and no part has been copied from any
other written or published source (except the references, standard
mathematical or genetics models / equation / formulas / protocol and
recommended book etc.).

Student Name:- Ali


Registration No:- 2022-GCUF-032323
Signature
4

Contents
Basics of Object Oriented Programming ....................................................................................................... 6
Object Oriented Programming: ................................................................................................................. 6
Program 13.1 ............................................................................................................................................. 6
Program 13.2 ............................................................................................................................................. 7
Program 13.3 ............................................................................................................................................. 8
Program 13.4 ............................................................................................................................................. 9
Constructor: ............................................................................................................................................ 11
Program 13.7 ........................................................................................................................................... 11
Program 13.8 ........................................................................................................................................... 12
Constructor Overloading: ........................................................................................................................ 13
Program 13.11 ......................................................................................................................................... 13
Default Copy Constructor:....................................................................................................................... 14
Program 13.12 ......................................................................................................................................... 14
Destructors:............................................................................................................................................. 16
Program 13.13 ......................................................................................................................................... 16
Static Data Member: ............................................................................................................................... 17
Program 13.15 ......................................................................................................................................... 17
Program 13.16 ......................................................................................................................................... 18
Friend Function: ...................................................................................................................................... 19
Program 13.17 ......................................................................................................................................... 19
Static Function: ....................................................................................................................................... 21
Program 13.18 ......................................................................................................................................... 21
Operator Overloading ................................................................................................................................. 23
Operator Overloading: ............................................................................................................................ 23
Overloading ++ Operator: ....................................................................................................................... 23
Program 14.1 ........................................................................................................................................... 23
Program 14.2 ........................................................................................................................................... 24
Program 14.3 ........................................................................................................................................... 25
Arithmetic Operators: ............................................................................................................................. 26
Program 14.4 ........................................................................................................................................... 26
Program 14.5 ........................................................................................................................................... 28
5

Inheritance .................................................................................................................................................. 30
Inheritance: ............................................................................................................................................. 30
15.1.1 Advantages of Inheritance ........................................................................................................... 30
1.Reusability ............................................................................................................................................ 30
2.Saves Time and Effort ........................................................................................................................... 30
3.Increases Program Structure and Reliability ........................................................................................ 30
Categories of Inheritance ........................................................................................................................ 30
1. Single Inheritance............................................................................................................................... 30
2. Multiple Inheritance........................................................................................................................... 31
Specifying a Derived Class ....................................................................................................................... 31
Syntax .................................................................................................................................................. 31
Program 15.1 ........................................................................................................................................... 32
Program 15.2 ........................................................................................................................................... 34
Program 15.3 ........................................................................................................................................... 36
POLYMORPHISM AND VIRTUAL................................................................................................................... 38
FUNCTIONS ................................................................................................................................................. 38
Polymorphism ......................................................................................................................................... 38
Program 16.1 .......................................................................................................................................... 38
Pointer to objects .................................................................................................................................... 39
Program 16.2 .......................................................................................................................................... 39
Array of pointers to objects .................................................................................................................... 40
Pointers and Inheritance ......................................................................................................................... 40
Virtual Functions ..................................................................................................................................... 41
Example ............................................................................................................................................... 41
Early Binding ........................................................................................................................................... 42
Late Binding............................................................................................................................................. 42
Pure Virtual Functions............................................................................................................................. 42
Abstract Classes ...................................................................................................................................... 43
Syntax .................................................................................................................................................. 43
6

Basics of Object Oriented


Programming

Object Oriented Programming:


Object-oriented programming (OOP) is a programming
paradigm that is based on the concept of "objects," which can contain data
in the form of fields, also known as attributes or properties, and code in
the form of procedures, often known as methods.

Program 13.1
Write a program that declares a class with one integer data member
and two member functions in() and out() to input and output data in data
member.

#include<iostream>
#include<conio.h>
using namespace std;
class test
{
private:
int n;
public:
void in()
{
cout<<"Enter Number: ";
cin>>n;
}
void out()
{
cout<<"The value of n = "<<n;
7

}
};
int main()
{
test obj;
obj.in();
obj.out();
}

Program 13.2
Write class marks with three data members to store three marks. Write
three member functions in() to input marks, sum() to calculate and return
the sum and avg() to calculate and return the average marks.

#include<iostream>
#include<conio.h>
using namespace std;
class marks
{
private:
int a,b,c;
public:
void in()
{
cout<<"Enter three number: ";
cin>>a>>b>>c;
}
int sum()
{
return a+b+c;
}
float avg()
{
return (a+b+c)/3.0;
8

}
};
int main()
{
marks m;
int s;
float a;
m.in();
s=m.sum();
a=m.avg();
cout<<"Sum ="<<s<<endl;
cout<<"Average ="<<a;

Program 13.3
Write a class circle with one data member radius. Write three member
functions get() radius to set radius value with parameter value, area() to
display radius and circum() to calculate and display circumference of
circle.

#include<iostream>
#include<conio.h>
using namespace std;
class circle
{
private:
float radius;
public:
void get_radius(float r)
{
radius=r;
}
void area()
9

{
cout<<"Area of circle: "<<3.14*radius*radius;
}
void circum()
{
cout<<"\nCircumference of circle: "<<2*3.14*radius;
}
};
int main()
{
circle c1;
float rad;
cout<<"Enter radius: ";
cin>>rad;
c1.get_radius(rad);
c1.area();
c1.circum();
}

Program 13.4
Write a class book with three data members BookID, Pages and
Price. It also contains the following member functions:
• The get() function is used to input values
• The show() function is used to display values
• The set() function is used to set values of data members using
parameters
• The getPrice() function is used to return values of price
The program should create two object of class and input values for these
objects. The program displays the details of most costly book.

#include<iostream>
#include<conio.h>
using namespace std;
class book
10

{
private:
int bookid,pages;
float price;
public:
void get()
{
cout<<"Enter book id: ";
cin>>bookid;
cout<<"Enter Pages: ";
cin>>pages;
cout<<"Enter Price: ";
cin>>price;
}
void show()
{
cout<<"Bookid ="<<bookid<<endl;
cout<<"Pages ="<<pages<<endl;
cout<<"Price ="<<price<<endl;
}
void set(int id,int pg,float pr)
{
bookid=id;
pages=pg;
price=pr;
}
float getprice()
{
return price;
}
};
int main()
{
book b1,b2;
b1.get();
11

b2.set(2,320,150.75);
cout<<"\nThe detail of most costly book is as follows: "<<endl;
if(b1.getprice()>b2.getprice())
b1.show();
else
b2.show();
}

Constructor:
A constructor is a special method or function that is automatically
called when an object is created. Its main purpose is to initialize the
object's attributes or properties and set up the object's initial state.
Constructors typically have the same name as the class to which they
belong.

Program 13.7
Write a class that displays a simple message on a screen whenever an
object of that class is created.

#include<iostream>
#include<conio.h>
using namespace std;
class Hello
{
private:
int n;
public:
Hello()
{
cout<<"Object created..."<<endl;
}
};
int main()
{
12

Hello x,y,z;
}

Program 13.8
Write a class that contains two integer data members which are
initialized to 100 when object is created. It has a member function avg
that displays average of data members.

#include<iostream>
#include<conio.h>
using namespace std;
class Number
{
private:
int x,y;
public:
Number()
{
x=y=100;
}
void avg()
{
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
cout<<"Average = "<<(x+y)/2<<endl;
}
};
int main()
{
Number n;
n.avg();
}
13

Constructor Overloading:
The process declaring multiple constructor with same name but
different parameters is known as constructor overloading. The constructor
with same name must differ in one of the following ways:
• Number of parameters
• Type of parameters
• Sequence of parameters

Program 13.11
Write a class that has num and ch as data members. A constructor
with no parameter initializes num to 0 and ch to ‘x’. A constructor with
two parameters initializes data members with given values and member
function shows displays the values of data members.

#include<iostream>
#include<conio.h>
using namespace std;
class Over
{
private:
int num;
char ch;
public:
Over()
{
num=0;
ch='x';
}
Over(int n,char c)
{
num=n;
ch=c;
}
void show()
14

{
cout<<"num = "<<num<<endl;
cout<<"ch = "<<ch<<endl;
}
};
int main()
{
Over first,second(100,'p');
cout<<"The contents of first:"<<endl;
first.show();
cout<<"The contents of second:"<<endl;
second.show();
}

Default Copy Constructor:


A type of constructor that is used to initialize an object with another
object of same type is known as default copy constructor. Its name is
“default copy constructor” because it is available by default in all classes.

Program 13.12
Write a class book that has attributes for pages, price and title. It has
two functions to input the values and displays the values. Create three
objects of class and input the values.

#include<iostream>
#include<conio.h>
using namespace std;
class Book
{
private:
int pg,pr;
15

char title[50];
public:
void get()
{
cout<<"Enter title: ";
gets(title);
cout<<"Enter pages: ";
cin>>pg;
cout<<"Enter price: ";
cin>>pr;
}
void show()
{
cout<<"Title: "<<title<<endl;
cout<<"Pages: "<<pg<<endl;
cout<<"Price: "<<pr<<endl;
}
};
int main()
{
Book b1;
b1.get();
Book b2(b1);
Book b3=b1;
cout<<"\nThe detail of b1:"<<endl;
b1.show();
cout<<"\nThe detail of b2:"<<endl;
b2.show();
cout<<"\nThe detail of b3:"<<endl;
b3.show();
}
16

Destructors:
A type of member function that is automatically executed when an
object of that class is destroyed is known as destructor. A destructor has
no return type and its name is same as class name. The destructor is
preceded by tilde sign ~.

Program 13.13
Write a class that displays a simple message on a screen whenever an
object of that class is created and destroyed.

#include<iostream>
#include<conio.h>
using namespace std;
class test
{
private:
int n;
public:
test()
{
cout<<"Object created..."<<endl;
}
~test()
{
cout<<"Object destoryed..."<<endl;
}
};
int main()
{
test a,b;
}
17

Static Data Member:


A type of data member that is shared among all objects of class is
known as static data member. The static data member is defined in class
the class with static keyword.

Program 13.15
Write a program that counts number of objects created of a particular
class.

#include<iostream>
#include<conio.h>
using namespace std;
class yahoo
{
private:
static int n;
public:
yahoo()
{
n++;
}
void show()
{
cout<<"You have created "<<n<<" object so far."<<endl;
}
};
int yahoo::n=0;
int main()
{
yahoo x,y;
x.show();
yahoo z;
z.show();
}
18

Program 13.16
Write a program that creates three objects of class student. Each
object of class must be assigned a unique roll number. (Hint: Use static
data member for unique roll number.)

#include<iostream>
#include<conio.h>
using namespace std;
class student
{
private:
static int r;
int rno,marks;
char name[1000];
public:
student()
{
r++;
rno=r;
}
void in()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter marks:";
cin>>marks;
}
void show()
{
cout<<"Roll No:"<<rno<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Marks:"<<marks<<endl;
}
19

};
int student::r=0;
int main()
{
student s1,s2,s3;
s1.in();
s2.in();
s3.in();
cout<<endl;
s1.show();
s2.show();
s3.show();
}

Friend Function:
A type of function that is allowed to access private and protected
members of a particular class from outside the class is called friend
function. A function that is declared in the class with friend keyword
becomes the friend function of the class.

Program 13.17
Write a program in which a friend function accepts two objects of
different classes as parameters. It has to process the private data members
of these classes and then displays the result.

#include<iostream>
#include<conio.h>
using namespace std;
class B;
class A
{
private:
int a;
public:
20

A()
{
a=10;
}
friend void show(A,B);
};
class B
{
private:
int b;
public:
B()
{
b=20;
}
friend void show(A,B);
};
void show(A x,B y)
{
int r;
r=x.a+y.b;
cout<<"value of class A object= "<<x.a<<endl;
cout<<"value of class B object= "<<y.b<<endl;
cout<<"sum of both values= "<<r<<endl;
}
int main()
{
A obj1;
B obj2;
show(obj1,obj2);
}
21

Static Function:
A type of member function that can be accessed without any object
of the class is called static function.

Program 13.18
Write a program that counts the number of objects created of a
particular class. The program must be able to display the result even if no
object is created so far.

#include<iostream>
#include<conio.h>
using namespace std;
class yahoo
{
private:
static int n;
public:
yahoo()
{
n++;
}
static void show()
{
cout<<"you have created "<<n<<" object so far."<<endl;
}
};
int yahoo::n=0;
int main()
{
yahoo::show();
yahoo x,y;
x.show();
yahoo z;
x.show();
22

}
23

Operator Overloading
Operator Overloading:
The process of defining additional meanings of operators is
known as operator overloading. It enables operators to perform different
operations depending on the type of operands. It also enables the operators
to process the user defined data types.

Overloading ++ Operator:
The increment operator ++ is unary operator. It works with single
operand. It increases the value of operand by 1. It only works with
numerical values by default.

Program 14.1
Write a program that overloads increment operator to work with user
defined objects.

#include<iostream>
#include<conio.h>
using namespace std;
class count
{
private:
int n;
public:
count()
{
n=0;
}
void show()
{
cout<<"n = "<<n<<endl;
}
24

void operator++()
{
n++;
}
};
int main()
{
count obj;
obj.show();
++obj;
obj.show();
}

Program 14.2
Write a program that overloads increment operator to work with user
defined objects. The overloaded function should return an object after
incrementing the data member.

#include<iostream>
#include<conio.h>
using namespace std;
class count
{
private:
int n;
public:
count()
{
n=0;
}
void show()
{
cout<<"n = "<<n<<endl;
}
25

count operator++()
{
count temp;
n=n+1;
temp.n=n;
return temp;
}
};
int main()
{
count x,y;
x.show();
y.show();
y=++x;
x.show();
y.show();
}

Program 14.3
Write a program that overloads postfix increment operator to work
with user defined objects.

#include<iostream>
#include<conio.h>
using namespace std;
class count
{
private:
int n;
public:
count()
{
n=0;
}
26

void show()
{
cout<<"n = "<<n<<endl;
}
count operator++()
{
count temp;
n=n+1;
temp.n=n;
return temp;
}
count operator++(int)
{
count temp;
n=n+1;
temp.n=n;
return temp;
}
};
int main()
{
count x;
x.show();
++x;
x++;
x.show();
}

Arithmetic Operators:
The binary arithmetic operators such as +, - ,*, / are binary operators.
They work with two operands.

Program 14.4
Write a program that overloads binary addition operator +.
27

#include<iostream>
#include<conio.h>
using namespace std;
class add
{
private:
int a,b;
public:
add()
{
a=b=0;
}
void in()
{
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
}
void show()
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
}
add operator+(add p)
{
add temp;
temp.a=p.a+a;
temp.b=p.b+b;
return temp;
}
};
int main()
{
28

add x,y,z;
x.in();
y.in();
z=x+y;
x.show();
y.show();
z.show();
}

Program 14.5
Write a program that overloads the comparison operators = = to work
with string class. The result of comparison must be 1 if two strings are of
same length and otherwise 0.

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class String
{
private:
char str[50];
public:
String()
{
str[0]='\0';
}
void in()
{
cout<<"Enter string: ";
gets(str);
}
void show()
{
29

cout<<str<<endl;
}
int operator == (String s)
{
if(string(s.str) == string(str))
return 1;
else
return 0;
}
};
int main()
{
String s1,s2;
s1.in();
s2.in();
cout<<"s1 = ";
s1.show();
cout<<"s2 = ";
s2.show();
if(s2==s1)
cout<<"Both strings are of diff length";
else
cout<<"Both strings are of equal length";
}
30

Inheritance
Inheritance:
A programming technique that is used to reuse an existing class to
build a new class is known as inheritance. The new class inherits all the
behavior of original class.

15.1.1 Advantages of Inheritance


Some important advantages of inheritance are as fellow:

1.Reusability
Inheritance allows the developer to reuse existing code in many
situations. A class can be created once and it can be reuse again and again
to create many sub classes.

2.Saves Time and Effort


Inheritance saves a lot of time and effort to write the same classes
again. The reusability of existing classes allows the program to work only
on new classes.

3.Increases Program Structure and Reliability


A super class is already compiled and tested properly. This class can be
used in a new application without compiling it again. The use of existing
class increases program reliability.

Categories of Inheritance
There are two categories of inheritance:

1. Single Inheritance
A type of inheritance in which a child class is derived from single parent
class is known as single inheritance. The child class inheritance inherits
31

all data members and member functions of the parent class. It can also add
further capabilities of its own.

Parent

Child

Figure 15.2: Single Inheritance

2. Multiple Inheritance
A type of inheritance in which a child class is derived from multiple
parent classes is known as multiple inheritance. The child class in this
inheritance inherits all data members and member functions of all parent
classes. It can also add further capabilities of its own.

Parent 1 Parent 2

Figure 15.3 Multiple


Child Inheritance

Specifying a Derived Class


The process of specifying derived class is same as specifying simple
class. Additionally, the reference of parent is specified along with derived
class name to inherit the capabilities of parent class.

Syntax
The syntax of specifying a derived class is as fellows:
Class sub _ class : specifier parent_class
{
body of the class
32

};

Program 15.1
Write a class person that has the attributes of id, name and address. It
has constructor to initialize, a member function to input and a member
function to display data members. Create another class student that
inherits person class. It has additional attributes roll number and marks. It
has member function to input and display its data members.

#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
class person
{
protected:
int id;
char name[50], address[100];
public:
person()
{
id=0;
name[0]='\0';
address[0]='\0';
}
void getinfo()
{
cout<<"Enter your id: ";
cin>>id;
cout<<"Enter your name: ";
gets(name);
cout<<"Enter your address: ";
33

gets(address);
}
void showinfo()
{
cout<<"\nYour personal information is as follows:\n";
cout<<"id = "<<id<<endl;
cout<<"Name = "<<name<<endl;
cout<<"Address = "<<address<<endl;
}
};
class student : public person
{
private:
int rno, marks;
public:
student()
{
person::person();
rno=marks=0;
}
void getedu()
{
cout<<"Enter your roll no: ";
cin>>rno;
cout<<"Enter your marks: ";
cin>>marks;
}
void showedu()
{
cout<<"\nYour educational information is as follows:\n";
cout<<"Roll No = "<<rno<<endl;
cout<<"Marks = "<<marks<<endl;
}
};
int main()
34

{
student s;
s.getinfo();
s.getedu();
s.showinfo();
s.showedu();
}

Program 15.2
Write a program that declare two classes. The parent class is called
Simple that has two data members a and b to store two numbers. It also
has four members function.

#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
class simple
{
protected:
int a,b;
public:
simple()
{
a=b=0;
}
void in()
{
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
}
35

void add()
{
cout<<"a+b = "<<a+b<<endl;
}
void sub()
{
cout<<"a-b = "<<a-b<<endl;
}
void mul()
{
cout<<"a*b = "<<a*b<<endl;
}
void div()
{
cout<<"a/b = "<<a/b<<endl;
}
};
class complex : public simple
{
public:
void add()
{
if(a<=0 || b<=0)
cout<<"Invalid values."<<endl;
else
simple::add();
}
void sub()
{
if(a<=0 || b<=0)
cout<<"Invalid values"<<endl;
else
simple::sub();
}
void mul()
36

{
if(a<=0 || b<=0)
cout<<"Invalid values"<<endl;
else
simple::mul();
}
void div()
{
if(a<=0 || b<=0)
cout<<"Invalid values"<<endl;
else
simple::div();
}
};
int main()
{
complex obj;
obj.add();
obj.in();
obj.add();
obj.sub();
obj.mul();
obj.div();
}

Program 15.3
Write a program that declare two classes and defines a relationship
between them using public inheritance.

#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
37

class parent
{
public:
int a;
protected:
int b;
private:
int c;
};
class child : public parent
{
public:
void in()
{
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
}
void out()
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
}
};
int main()
{
child obj;
obj.in();
obj.out();
}
38

POLYMORPHISM AND VIRTUAL


FUNCTIONS
Polymorphism
The word polymorphism is a combination of two words poly and
morphism. Poly means many and morphism means form. In object-
oriented programming, polymorphism means that an object may behave
differently in different conditions. It allows a programmer to reuse and
extend the code. The same code acts differently depending on the
situation.

Program 16.1
Write a class with an integer data member , a function to input and a
function to display it. Creates an object of the class using pointer and class
its member functions.

#include<iostream>
#include<conio.h>
using namespace std;
class Test
{
private:
int n;
public:
void in()
{
cout<<"Enter number ="; cin>>n;
}
void out()
{
cout <<"The value of n ="<<n;
}
39

};
int main()
{
Test *ptr;
ptr= new Test;
ptr ->in();
ptr->in();
ptr->out();
}

Pointer to objects
A pointer can also refer to an object of a class. The member of an
object can be accessed through pointers by using the symbol ->. The
symbol is known as member access operator.

Syntax
The syntax of referencing an object with pointer is as follows:
Ptr -> member

Program 16.2
Write a class that contains an attribute name, a function to input and a
function to display name. Creates array of pointers in which each element
refers to an object of the class.
#include<iostream> #include<conio.h> using namespace std; class
Person
{
private: char name[50];
public:
void get()
{
cout<<"Enter your name:
"; cin>>name;
}
void show()
40

{
cout<<"Your name ="<<name<<endl;
}
};
int main()
{
Person *ptr[5];
int i;
for(i=0; i<5; i++)
{
ptr[i] =new Person; ptr[i]-
>get();
}
for(i=5; i<5; i++) ptr[i]-
>show();
}

Array of pointers to objects


An array can store same type of data. An array of pointers can store
member addresses of the objects of same class. It allows the user to create
a large number of objects in memory using new operator and process then
easily using loops. Each element of the array will refer to a different object
in the memory

Pointers and Inheritance


Pointers have very important capability of storing the addresses of
different objects. A pointer can store the address of object whose type is
same as the type of pointer. It can also store the address of any object that
belongs to any child class of the pointer’s class. Suppose there are three
classes A, B and C. The class A is a parent class whereas B and C are child
classes. A pointer ptr of class A can store the addresses of all objects of A,
B and C.
41

Virtual Functions
Virtual means existing in effect but not in reality. A type of function
that appears to exist in some part of a program but does not exist really is
called virtual function. Virtual functions are used to implement
polymorphism. A virtual function is defined in the parent class and be
overridden in child classes. It is defined by using the keyword virtual.

Example
#include<iostream>
#include<conio.h>
using namespace std;
class A
{
public:
virtual void show()
{
cout<<"Parent class A..."<<endl;
}
};
class B : public A
{ public:
void show()
{
cout<<"Child class B..."<<endl;
}
};
class C : public A
{
public: void show()
{
cout<<"Child class C..."<<endl;
}
};
int main()
42

{
obj1;
obj2;
obj3;
A *ptr; ptr = &obj1;
ptr>show();
ptr = &obj2;
ptr->show();
ptr = &obj3;
ptr->show();
getch();
}

Early Binding
The assignment of types to variables and expressions at compilation
time is known as early binding. It is also called static binding. The early
binding occurs when everything required to call a function is known at
compile time. Early binding enables the compiler to know exactly which
function will be called when a certain statement is executed.

Late Binding
The assignment of types to variables and expressions at execution
time is known as late binding. It is also called dynamic binding. The late
binding occurs when some information to call a function is decided at run
time. The compiler does not know at compile time which function will
be executed. It provides more flexibility.

Pure Virtual Functions


A type of virtual function that has no body is known as pure virtual
function. A function cab be declared as pure virtual function by adding
two things:
The keyword virtual at the start of the function declarator
The = 0 at the end of function declarator
43

Abstract Classes
A type of class that contains any pure virtual function is called
abstract class. An abstract class cannot be used directly. It means that
no object of an abstract class can be created. However, a child class can
inherit an abstract class and use it by overriding its pure virtual fuction.

Syntax
The syntax of declaring pure virtual function is as follows:
virtual return –type function-name () = 0;

Example
#include<iostream> Parent
#include<conio.h>
using namespace std;
class Parent
{
Child1 Child2
private:
int n;
};
class Child1 : public Parent
{ }; Baby
class Child2 : public Parent
{ };
class Baby : public Child1, public Child2
{
public: void set()
{
n= 10; cout<<”n = ”<<n>>endl;
}
};
int main()
{
44

Baby obj;
obj.set();
getch();
}

You might also like