0% found this document useful (0 votes)
66 views28 pages

Object Oriented Programming (Oop) With C++

This document discusses object oriented programming concepts in C++ including virtual functions, polymorphism, and inheritance. It covers: - Pointers to objects and accessing members through object pointers - Object pointers in inheritance and how base class pointers can point to derived class objects - Derived class pointers can only point to derived class objects, not base class objects - Dynamic binding, late binding, and pure virtual functions as it relates to polymorphism The document contains code examples demonstrating these OOP concepts. It is presented by Dr. Mahbubul Syeed from the Computer Science department at AIUB for their Fall 2017 semester.

Uploaded by

ANIK Dey
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)
66 views28 pages

Object Oriented Programming (Oop) With C++

This document discusses object oriented programming concepts in C++ including virtual functions, polymorphism, and inheritance. It covers: - Pointers to objects and accessing members through object pointers - Object pointers in inheritance and how base class pointers can point to derived class objects - Derived class pointers can only point to derived class objects, not base class objects - Dynamic binding, late binding, and pure virtual functions as it relates to polymorphism The document contains code examples demonstrating these OOP concepts. It is presented by Dr. Mahbubul Syeed from the Computer Science department at AIUB for their Fall 2017 semester.

Uploaded by

ANIK Dey
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/ 28

OBJECT ORIENTED PROGRAMMING (OOP) WITH C++

VIRTUAL FUNCTION, OBJECT POINTERS.


DYNAMIC LINKAGE, LATE BINDING, POLYMORPHISM, PURE VIRTUAL FUNCTION.

AIUB. FALL 2017

Dr. Mahbubul Syeed


Associate Professor, Department. of CS, AIUB
[email protected]
www.msyeed.weebly.com
CONTENTS

ü Pointer to objects
ü Pointer to objects in inheritance
ü Virtual function
ü Polymorphism:
ü dynamic linkage
ü late binding.
ü Pure virtual function.
POINTER TO OBJECTS
POINTER TO OBJECTS
C++ allows you to have pointers to objects. The pointers pointing to objects are referred to as Object Pointers.

C++ Declaration of Object pointer:


class-name ∗ object-pointer ;
where class-name is the name of an already defined class and object-pointer is the pointer to an object of this class type.

Accessing members of a class using Object pointer:


object-pointer -> memberOfTheClass;

where memberOfTheClass is any public member function or varibale with the class.

Or,
(*object-pointer).memberOfTheClass;
#include <iostream>
#include <string>
using namespace std; student(int r, string n){
class student{
rollno = r;
private: name = n;
int rollno; }
string name;
public:
student():rollno(0),name("") {}
student(int r, string n): rollno(r),name (n) {}
int main (){
void get() {
student st1;
cout<<"enter roll no: ";
cin >> rollno; student *stPtr;
cout<<"\nenter name: ";
cin >> name; } stPtr = &st1;
stPtr->get();
void print() { stPtr->print();
cout<<"You have entered... \n roll no: "<< rollno;
cout<<"\n name: "<< name; }
//following two statemets are also valid
};
// (*stPtr).get();
// (*stPtr).print();
}
Creating Dynamic Object
#include <iostream>
#include <string>
using namespace std;
class student
{
private:
int rollno;
string name; void main ()
public: {
student():rollno(0),name("") student *ps=new student;
{} (*ps).get();
student(int r, string n): rollno(r),name (n) (*ps).print();
{} delete ps;
void get() }
{
cout<<"enter roll no";
cin>>rollno;
cout<<"enter name";
cin>>name;
}
void print()
{
cout<<"roll no is "<<rollno;
cout<<"name is "<<name;
}
};
POINTER TO OBJECTS - INHERITANCE

Class: Base Base base;


Derived derived;

Base *basePtr;
Class: Derived
basePtr = &base;
basePtr = &derived;

A base class pointer can point to both the base class and derived class objects!!
#include<iostream> Base class pointer pointing to derived class object
using namespace std;

class Base{
int b;
public:
Base(int b=0):b(b){} int main(){
Base *basePtr;
void show() { from baseclass, b = 10
cout <<"from baseclass, b = " <<b<<endl; }
};
Base base(10); from baseclass, b = 0
Derived derived(20);
class Derived : public Base{
int d; basePtr = &base;
public: basePtr->show();
Derived(int d=0):d(d){}

void show() { basePtr = &derived;


cout << "from derived class.."<<endl; basePtr->show();
Base::show(); }
cout << "in derived class, d = "<<d<<endl;
}
};
Base class pointer can not resolve derive class object by default.
#include<iostream> Base class pointer pointing to derived class object
using namespace std; Through Casting
class Base{
int b;
public:
Base(int b=0):b(b){} int main(){
Base *basePtr;
void show() { from baseclass, b = 10
cout <<"from baseclass, b = " <<b<<endl; }
};
Base base(10); from derived class..
Derived derived(20); from baseclass, b = 0
class Derived : public Base{ in derived class, d = 20
int d; basePtr = &base;
public: basePtr->show();
Derived(int d=0):d(d){}

void show() { basePtr = &derived;


cout << "from derived class.."<<endl; (Derived*)basePtr)->show();
Base::show(); }
cout << "in derived class, d = "<<d<<endl;
}
}; Base class pointer can resolve derive class object if its explicity casted
To derived class object.
POINTER TO OBJECTS - INHERITANCE

Class: Base Base base;


Derived derived;

Derived *derivedPtr;
Class: Derived
derivedPtr = &base; // not possible!!!
derivedPtr = &derived;

A derived class pointer can never point to the base class objects!!
A derived class pointer can only point to derived class objects!
#include<iostream> Derived class pointer can never point to base class object.
using namespace std;

class Base{
int b;
public:
Base(int b=0):b(b){} int main(){
Derived *derivedPtr;
void show() {
cout <<"from baseclass, b = " <<b<<endl; }
In function 'int main()': 34:16: error:
};
Base base(10); invalid conversion from 'Base*' to
Derived derived(20); 'Derived*' [-fpermissive]
class Derived : public Base{
int d; derivedPtr = &derived;
public: derivedPtr->show();
Derived(int d=0):d(d){}

void show() { derivedPtr = &base;


cout << "from derived class.."<<endl; derivedPtr->show();
Base::show(); }
cout << "in derived class, d = "<<d<<endl;
}
};
AN EXERCISE
What is the output??
int main()
{
class B B *pb;
{ B bb(5);
int b;
public: cout << "Pointer of B refers to the object of B" << endl;
B(int b=0):b(b){} pb = & bb;
void show() pb->show();
{
cout <<"b = " <<b<<endl; cout << "Pointer of B refers to the object of B without casting" << endl;
} D dd(10);
}; pb = &dd;
pb->show();
class D : public B
{ cout << "Pointer of D refers to the object of D" << endl;
int d; D *pd;
public: pd = &dd;
D(int d=0):d(d){} pd->show();
void show()
{
cout << "Pointer of B refers to the object of D after casting" << endl;
B::show();
((D *)pb)->show();
cout << "d = "<<d<<endl;
}
return 0;
};
}
Pointer of B refers to the object of B
b = 5

Pointer of B refers to the object of B without casting


b = 0

Pointer of D refers to the object of D


b = 0
d = 10

Pointer of B refers to the object of D after casting


b = 0
d = 10
VIRTUAL FUNCTION
AN ALTERNATE TO CASTING BASE CLASS POINTER !!!
VIRTUAL FUNCTION

¡ In case of base class pointer object, if the base class function is declared as virtual

¡ Then C++ can determine in runtime which function to call (from base class or from derived
class) based on the object pointed by the base class pointer.

• The function in the base class is declared as virtual by using the keyword virtual preceding its
normal declaration.
#include<iostream> Base class pointer pointing to derived class object
using namespace std; With virtual functions in base classes.
class Base{
int b;
public:
Base(int b=0):b(b){} int main(){
Base *basePtr;
void virtual show() { from baseclass, b = 10
cout <<"from baseclass, b = " <<b<<endl; }
};
Base base(10); from derived class..
Derived derived(20); from baseclass, b = 0
class Derived : public Base{ in derived class, d = 20
int d; basePtr = &base;
public: basePtr->show();
Derived(int d=0):d(d){}

void show() { basePtr = &derived;


cout << "from derived class.."<<endl; basePtr->show();
Base::show(); }
cout << "in derived class, d = "<<d<<endl;
}
};
Check yourself!!
class B
{
int b; int main()
{
public: B *pb;
B(int b=0):b(b){} B bb(5);
virtual void show()
cout << "Pointer of B refers to the object of B" << endl;
{
cout <<"b = " <<b<<endl; pb = & bb;
pb->show();
}
}; cout << "Pointer of B refers to the object of D (Dynamic Binding) " << endl;
D dd(10);
pb = &dd;
class D : public B pb->show();
{
int d; cout << "Pointer of D refers to the object of D" << endl;
D *pd;
public: pd = &dd;
D(int d=0):d(d){} pd->show();
void show() cout << "Pointer of B refers to the object of D after casting" << endl;
{ ((D *)pb)->show();
B::show();
return 0;
cout << "d = "<<d<<endl; }
}
};
Pointer of B refers to the object of B
b = 5

Pointer of B refers to the object of D (Dynamic


Binding)
b = 0
d = 10

Pointer of D refers to the object of D


b = 0
d = 10

Pointer of B refers to the object of D after casting


b = 0
d = 10
POLYMORPHISM
Polymorphism means same action but different reaction/reply.
Or
Same entity having different forms!!
In C++, polymorphism refers to the property by which objects belonging to different classes are able to
respond to the same message, but in different forms.

Polymorphism is also known as late binding/dynamic binding/run-time binding.

In C++, two things are required to achieve polymorphism


1. A virtual function in the base class.
2. A pointer of the base class.
PURE VIRTUAL FUNCTION & ABSTRACT CLASS
q A class / Base class with only definitions in it (i.e., no code implementation) is called abstract
class.

q We cannot create objects of an abstract class.

q Pure virtual function:

q A pure virtual function is used to make a class abstract.

q A virtual function is made ‘pure virtual’ by assigning zero(0) to the function name. Such a
function is also known as ‘do-nothing’ function.

q e.g., virtual void show() = 0;


class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};

class Derived:public Base


{
public:
void show()
{ cout << "Implementation of Virtual Function in Derived class"; }
};

int main()
{
Base obj; //Compile Time Error
Base *b;
Derived d;
b = &d;
b->show();
}
class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};

class Derived1:public Base {


public:
void show(){ cout << "Implementation of Virtual Function in Derived class 1 \n";}
};

class Derived2:public Base{


public:
void show(){ cout << "Implementation of Virtual Function in Derived class 2"; }
};

int main(){
Base *b;
Derived1 d1;
Derived2 d2;
b = &d1;
b->show();
b = &d2;
b->show();
}
ALL ABOUT VIRTUAL

- Virtual keyword is used with derived class to create only one instance of base class in
case of multiple inheritance.

- Virtual keyword is used with base class function name to implement Polymorphism /
dynamic linking / runtime binding. In polymorphism a pointer object of a base class can
resolve in runtime which function to call based on the actual object its pointing to!

- Pure virtual function is used to declare an abstract base class.


ASSIGNMENT
Person
-------------------------------------------------- Consider this multiple and
Name multilevel
Gender Inheritance scenario for an
-------------------------------------------------- hypothetical
Default constructor to set default values Department! J
Setter and getter functions

Teacher Student
-------------------------------------------------- --------------------------------------------------
Department Student id
Courses CGPA
-------------------------------------------------- --------------------------------------------------
Default constructor to set default values Default constructor to set default values
Setter and getter functions Setter and getter functions

TeachingAssistant
--------------------------------------------------
Semester
--------------------------------------------------
Default constructor to set default values
Setter and getter functions
Your task is as followings:
1. Write a C++ program to implement the given inheritance scenario.You have to implement all the
classes, for each class implement all the attributes and functions as described.
2. Make sure your program handles all the concerns that may occur due to multiple inheritance as
discussed in the class.
3. Create an array of three TA objects and set all the values for each of the TA objects, eg., name, gender,
dept and others.
4. Now, Print all the information of the TA who has highest CGPA among the three.

Submission detail:
- Deadline:28.11.2017 (11:00 AM)
- Email attachment (.txt file)
- Email Subject: assignment8_PL2
- Write your name, id and section in the email body

For Section A+B1: Send your assignment to the following email address: [email protected]
For Section C+B2: Send your assignment to the following email address: [email protected]
Student
-------------------------------------------------- Consider this multiple and
name multilevel
id Inheritance scenario for an
-------------------------------------------------- hypothetical
Default constructor to set default values Department! J
Setter and getter functions

Exam Sports
-------------------------------------------------- --------------------------------------------------
courseName sportName
marks marks
-------------------------------------------------- --------------------------------------------------
Default constructor to set default values Default constructor to set default values
Setter and getter functions Setter and getter functions

Result
--------------------------------------------------
totalMarks
--------------------------------------------------
float getResults(){
// this function will add both marks from Exam and Sports class,
store it in totalMarks and return it.}

You might also like