Programming fundamental
Submitted by : Sidra Tahir
Submitted to : Ma'am Shumaila
Subject : object oriented program
Roll no : 165
Class : BSCS
Date : 30- 03-2020
Session : 2019-2023
COVT. POST GRADUATE COLLEGE FOR WOMENS GULBERG LAHORE
CONTENTS
 Polymorphism
 Virtual function
 Pointer to all object
 Abstractconcrete class
 Pure virtual class
PowerPlugs Templates for PowerPoint Preview 3
Polymorphism
In object-oriented
programming, polymorphism ref
ers to a programming language's
ability to process objects
differently depending on their
data type or class. More
specifically, it is the ability to
redefine methods for derived
classes.
Real life Example
 A person at the same time can have different characteristic.
Like a man at the same time is a father, a husband, an
employee. So the same person posses different behavior in
different situations. This is called polymorphism.
you have a smartphone for communication. The
communication mode you choose could be anything. It can be
a call, a text message, a picture message, mail, etc. So, the goal
is common that is communication, but their approach is
different.
Example Of Polymorphism
#include<iostream.h>
#include<conio.h>
class test
{
clrscr();
private:
int n;
public:
void in()
{
cout<<"enter a number:";
cin>>n;
}
Continue……………………
void out()
{
cout<<"the value of n = "<<n;
}
};
void main()
{
clrscr();
test*ptr;
ptr=new test;
ptr->in();
ptr->out();
getch();
}
PowerPlugs Templates for PowerPoint Preview 7
Output and working of program
output
Working:
The above program declares a class test and a pointer ptr of type test. The pointer can
refer to object of the class. The new operator creates an object in the memory and stores
the address in ptr. The program then uses the pointer to call the member function of the
object being referenced by the pointer. The symbol -> is used to access the member of any
object through pointer. The use of dot operator with pointer to access any member is
invalid.
 Virtual means existing in effect but not really.
 A type of function in some part of a program but does not exist really is
called virtual function.
 A virtual function is defined in the present class and can be overridden in
child classes.
Virtual function
Class class-name
{
Public:
Virtual void member- function-name()
{
…………. …………... body of base class
}
};
Syntax of virtual function
keyword
#include<iostream.h>
#include<conio.h>
class cbaseclass
{
public:
virtual void sayhello()
{
cout<<"hello from the cbaseclassnn";
}
void sayhi()
{
cout<<" hi from cbaseclassnn";
}
Example
program
#include<iostream.h>
#include<conio.h>
class a
{
public:
void disp()
{
cout<<" n i am from base class a";
}
};
class b : public a
{
public:
void disp()
{
cout<<"n i am from base class b";
}
};
continue……….
PowerPlugs Templates for PowerPoint Preview 12
class c: public b
{
public:
void disp()
{
cout<<"n i am from base class c";
}
};
void main()
{
a *pa;
b objb;
c objc;
pa=&objb;
pa->disp();
pa=&objc;
pa->disp();
getch();
}
PowerPlugs Templates for PowerPoint Preview 13
Output of program
output
i am from base class a
i am from base class a
PowerPlugs Templates for PowerPoint Preview 14
Working of program
The program declares three class a are the parent class and b and c are the child
classes. Each class has a member function disp().The disp() function in parent class a is
declared as virtual function. The child classes b and c override that member function.
The program declares one object of each class. It also declares a pointer ptr of class a.
the address of each object is stored in pointer and disp(0 function is called . Each time
the disp() function is called, corresponding function execution is executed
PowerPlugs Templates for PowerPoint Preview 15
A type of virtual function that has no body is known as pure virtual
function. A function can 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 declaratory.
Note: the pure virtual function are used in the classes which are not
used directly. The user can inherit the class and then override the pure
virtual function in the child class.
Pure virtual function
Class class-name
{
Public:
Virtual return-type function-name()=0
};
syntax
keyword
=0 indicate
that function
is pure
virtual
function
#include<iostream.h>
#include<conio.h>
Class A
{
clrscr();
Public:
Virtual void disp()=0;
};
Class B : public A
{
void disp()
{
Cout <<“n I am form derived class B”;
}};
Program and Example
Class C :public B
{
Public:
Void disp()
{
Cout<<“n I am form derived class
C”;
}
};
Int main()
{
B obj b;
C obj c;
Obj b.disp)();
Obj c.disp();
getch();
}
};
Output and working of program
Output
Working:
The program declares three class A are the parent class and B and C are
the child classes. Each class has a member function disp().The disp()
function in parent class A is declared as pure virtual function. The child
classes b and c override that member function.
The program declares one object of each class and disp(0 function is called
. Each time the disp() function is called, corresponding function execution
is executed
PowerPlugs Templates for PowerPoint Preview 19
Abstract class
By definition, an abstract class in C++ is a class that has at
least one pure virtual function (i.e., a function that has no
definition).
Syntax
abstract Class < class-name>
{
Public:
Abstract void function-name()
{
……….
}
Abstract method
}
PowerPlugs Templates for PowerPoint Preview 21
keyword
Program and Example
#include<iostream.h>
#include<conio.h>
Class abstract interface
{
Public:
Virtual void numbers()=0;
Void input();
Int a,b;
};
Void abstract interface : : input
{
Cout<<“enter the two numbers”;
Cin>>a>>b;
}
Continue………………..
PowerPlugs Templates for PowerPoint Preview 22
Class add: public abstract interface
{
Public:
Void numbers()
{
Int sum,a,b;
Sum=a+b;
Cout<<“sum is << sum<<“n”;
}
};
Class sub: public abstract interface
{
Public:
Void numbers()
{
Cout<<“diff is << diff <<“n”;
}
};
PowerPlugs Templates for PowerPoint Preview 23
int main()
{
Output
Add obj1;
Obj1 input();
Obj1 numbers();
Sub obj2;
Obj2 input();
Obj2 numbers();
}
output
Working of program
The program declares parent as abstract class interface and the child classes
are add and subclass. The child class add override the pure virtual function
declared in parent class. It is important to declare a pure virtual function in
parent class and override it in all child classes to implement polymorphism.
Takin g input in both child classes and then in main function made objects of
both child classes as input and numbers.
PowerPlugs Templates for PowerPoint Preview 24
Abstract class  concrete class
• Concrete classes are not
interface.
• Concrete classes can be base or
derived.
• Pure virtual function are may
not present in concrete
class.these classes can
instantiate the objects.
PowerPlugs Templates for PowerPoint Preview 25
• Abstract classes are the
interface
• Abstract class is generally a base
class.
• Pure virtual function should be
present in the abstract class.
• This classes cannot instantiate
the facts.
Shape
2D shape 3D SHAPE
SQAURE
CIRCLE TRIANGLE
CUBE SPHERE
CYLINDER
ABSTACT
CLASSES
CONCRETE
CLASSES
Pointer to call objects
 When accessing members of a class given a pointer to an object, use the arrow (-
>) operator instead of dot (.) operator.
 Must initialize the pointer before using it.
 Pointer athematic is relative to the base type of the pointer.
syntax
ptr -> member
PowerPlugs Templates for PowerPoint Preview 27
It is the name of pointer that reference an object.
It is the member access operator that is used to access a member of
object.
It is the class member to be accessed.
Program
#include<iostream.h>
#incl;ude<conio.h>
Class laptop
{
Int ram;
Public:
Int usb;
Void getdata (int a)
{
Ram =a;
}
Void disp()
{
Cout<<“ram : ” << ram<< endl;
Cout<<“usb : ”<< usb < <endl;
} };
PowerPlugs Templates for PowerPoint Preview 28
ntI main()
{
Laptop dell
Laptop *pdell = &dell;
Pdell -> usb = 3;
Pdell->getdata(20);
Pdell-> disp()
Getch();
}
Output
Working of program
The program declares a class laptop and a pointer pdell of type laptop. The pointer
refer the object of the class. The new operator creates an object in the memory and
stores the address in pdell. The program then uses the pointer to call the member
function of the object being referenced by the pointer. The symbol -> is used to access
the member of any object through pointer.
PowerPlugs Templates for PowerPoint Preview 29
PowerPlugs Templates for PowerPoint Preview 30

Virtual function

  • 2.
    Programming fundamental Submitted by: Sidra Tahir Submitted to : Ma'am Shumaila Subject : object oriented program Roll no : 165 Class : BSCS Date : 30- 03-2020 Session : 2019-2023 COVT. POST GRADUATE COLLEGE FOR WOMENS GULBERG LAHORE
  • 3.
    CONTENTS  Polymorphism  Virtualfunction  Pointer to all object  Abstractconcrete class  Pure virtual class PowerPlugs Templates for PowerPoint Preview 3
  • 4.
    Polymorphism In object-oriented programming, polymorphismref ers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.
  • 5.
    Real life Example A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism. you have a smartphone for communication. The communication mode you choose could be anything. It can be a call, a text message, a picture message, mail, etc. So, the goal is common that is communication, but their approach is different.
  • 6.
    Example Of Polymorphism #include<iostream.h> #include<conio.h> classtest { clrscr(); private: int n; public: void in() { cout<<"enter a number:"; cin>>n; } Continue……………………
  • 7.
    void out() { cout<<"the valueof n = "<<n; } }; void main() { clrscr(); test*ptr; ptr=new test; ptr->in(); ptr->out(); getch(); } PowerPlugs Templates for PowerPoint Preview 7
  • 8.
    Output and workingof program output Working: The above program declares a class test and a pointer ptr of type test. The pointer can refer to object of the class. The new operator creates an object in the memory and stores the address in ptr. The program then uses the pointer to call the member function of the object being referenced by the pointer. The symbol -> is used to access the member of any object through pointer. The use of dot operator with pointer to access any member is invalid.
  • 9.
     Virtual meansexisting in effect but not really.  A type of function in some part of a program but does not exist really is called virtual function.  A virtual function is defined in the present class and can be overridden in child classes. Virtual function
  • 10.
    Class class-name { Public: Virtual voidmember- function-name() { …………. …………... body of base class } }; Syntax of virtual function keyword
  • 11.
    #include<iostream.h> #include<conio.h> class cbaseclass { public: virtual voidsayhello() { cout<<"hello from the cbaseclassnn"; } void sayhi() { cout<<" hi from cbaseclassnn"; } Example
  • 12.
    program #include<iostream.h> #include<conio.h> class a { public: void disp() { cout<<"n i am from base class a"; } }; class b : public a { public: void disp() { cout<<"n i am from base class b"; } }; continue………. PowerPlugs Templates for PowerPoint Preview 12
  • 13.
    class c: publicb { public: void disp() { cout<<"n i am from base class c"; } }; void main() { a *pa; b objb; c objc; pa=&objb; pa->disp(); pa=&objc; pa->disp(); getch(); } PowerPlugs Templates for PowerPoint Preview 13
  • 14.
    Output of program output iam from base class a i am from base class a PowerPlugs Templates for PowerPoint Preview 14
  • 15.
    Working of program Theprogram declares three class a are the parent class and b and c are the child classes. Each class has a member function disp().The disp() function in parent class a is declared as virtual function. The child classes b and c override that member function. The program declares one object of each class. It also declares a pointer ptr of class a. the address of each object is stored in pointer and disp(0 function is called . Each time the disp() function is called, corresponding function execution is executed PowerPlugs Templates for PowerPoint Preview 15
  • 16.
    A type ofvirtual function that has no body is known as pure virtual function. A function can 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 declaratory. Note: the pure virtual function are used in the classes which are not used directly. The user can inherit the class and then override the pure virtual function in the child class. Pure virtual function
  • 17.
    Class class-name { Public: Virtual return-typefunction-name()=0 }; syntax keyword =0 indicate that function is pure virtual function
  • 18.
    #include<iostream.h> #include<conio.h> Class A { clrscr(); Public: Virtual voiddisp()=0; }; Class B : public A { void disp() { Cout <<“n I am form derived class B”; }}; Program and Example Class C :public B { Public: Void disp() { Cout<<“n I am form derived class C”; } }; Int main() { B obj b; C obj c; Obj b.disp)(); Obj c.disp(); getch(); } };
  • 19.
    Output and workingof program Output Working: The program declares three class A are the parent class and B and C are the child classes. Each class has a member function disp().The disp() function in parent class A is declared as pure virtual function. The child classes b and c override that member function. The program declares one object of each class and disp(0 function is called . Each time the disp() function is called, corresponding function execution is executed PowerPlugs Templates for PowerPoint Preview 19
  • 20.
    Abstract class By definition,an abstract class in C++ is a class that has at least one pure virtual function (i.e., a function that has no definition).
  • 21.
    Syntax abstract Class <class-name> { Public: Abstract void function-name() { ………. } Abstract method } PowerPlugs Templates for PowerPoint Preview 21 keyword
  • 22.
    Program and Example #include<iostream.h> #include<conio.h> Classabstract interface { Public: Virtual void numbers()=0; Void input(); Int a,b; }; Void abstract interface : : input { Cout<<“enter the two numbers”; Cin>>a>>b; } Continue……………….. PowerPlugs Templates for PowerPoint Preview 22
  • 23.
    Class add: publicabstract interface { Public: Void numbers() { Int sum,a,b; Sum=a+b; Cout<<“sum is << sum<<“n”; } }; Class sub: public abstract interface { Public: Void numbers() { Cout<<“diff is << diff <<“n”; } }; PowerPlugs Templates for PowerPoint Preview 23 int main() { Output Add obj1; Obj1 input(); Obj1 numbers(); Sub obj2; Obj2 input(); Obj2 numbers(); } output
  • 24.
    Working of program Theprogram declares parent as abstract class interface and the child classes are add and subclass. The child class add override the pure virtual function declared in parent class. It is important to declare a pure virtual function in parent class and override it in all child classes to implement polymorphism. Takin g input in both child classes and then in main function made objects of both child classes as input and numbers. PowerPlugs Templates for PowerPoint Preview 24
  • 25.
    Abstract class concrete class • Concrete classes are not interface. • Concrete classes can be base or derived. • Pure virtual function are may not present in concrete class.these classes can instantiate the objects. PowerPlugs Templates for PowerPoint Preview 25 • Abstract classes are the interface • Abstract class is generally a base class. • Pure virtual function should be present in the abstract class. • This classes cannot instantiate the facts. Shape 2D shape 3D SHAPE SQAURE CIRCLE TRIANGLE CUBE SPHERE CYLINDER ABSTACT CLASSES CONCRETE CLASSES
  • 26.
    Pointer to callobjects  When accessing members of a class given a pointer to an object, use the arrow (- >) operator instead of dot (.) operator.  Must initialize the pointer before using it.  Pointer athematic is relative to the base type of the pointer.
  • 27.
    syntax ptr -> member PowerPlugsTemplates for PowerPoint Preview 27 It is the name of pointer that reference an object. It is the member access operator that is used to access a member of object. It is the class member to be accessed.
  • 28.
    Program #include<iostream.h> #incl;ude<conio.h> Class laptop { Int ram; Public: Intusb; Void getdata (int a) { Ram =a; } Void disp() { Cout<<“ram : ” << ram<< endl; Cout<<“usb : ”<< usb < <endl; } }; PowerPlugs Templates for PowerPoint Preview 28 ntI main() { Laptop dell Laptop *pdell = &dell; Pdell -> usb = 3; Pdell->getdata(20); Pdell-> disp() Getch(); } Output
  • 29.
    Working of program Theprogram declares a class laptop and a pointer pdell of type laptop. The pointer refer the object of the class. The new operator creates an object in the memory and stores the address in pdell. The program then uses the pointer to call the member function of the object being referenced by the pointer. The symbol -> is used to access the member of any object through pointer. PowerPlugs Templates for PowerPoint Preview 29
  • 30.
    PowerPlugs Templates forPowerPoint Preview 30