You are on page 1of 28

OBJECT ORIENTED PROGRAMMING (OOP) WITH C++

STATIC DATA MEMBER


DATA TYPES IN FUNCTION OVERLOADING
INTRODUCTION TO INHERITANCE

AIUB. FALL 2017

Dr. Mahbubul Syeed


Associate Professor, Department. of CS, AIUB
mahbubul.syeed@aiub.edu
www.msyeed.weebly.com
CONTENTS

ü Class members: why and how (Static data members and functions).
ü Passing object as function argument.
ü Solving puzzle with data types in function overloading.
ü Inheritance (What, why and how)
ü Base class and derived class in relation to inheritance (real life example)
ü What is inherited in inheritance
ü Access control in Inheritance (public, private and protected)
ü Multiple inheritances.
ü Constructor and destructor role in inheritance.
CLASS MEMBERS
STATIC DATA MEMBER (VARIABLE) OF A CLASS

ü A static member is shared by all objects of the class and associated with the class.
ü When we declare a member of a class as static it means no matter how many objects of the class are
created, there is only one copy of the static member.

ü We can define class members static using static keyword.

ü A static data member can be initialized outside class declaration with scope resolution operator.
ü All static data is initialized to zero when the first object is created (if no other initialization is present).
class item
{ Count = 021
private: int main()
double price;
static int count; {
public: item t1;
void getdata() item t2;
{
cin>>price; t1.getdata();
count++; t1.showcount(); t1 t2
} ----- -----
t2.showcount();
void putdata() price price
t2.getdata();
{
cout<<"price="<<price<<endl; t2.showcount();
}
t1.showcount();
void showcount() return 0;
{
cout<<"count="<<count<<endl; }
}
};

int item::count;
A PUBLIC STATIC MEMBER VARIABLE
EXAMPLE
#include <iostream>
using namespace std; int main(){
Student s1("xyz", "17-111111-1", "cs");
Student::Student(){} Student s2("abc", "17-222222-1", "cse");
class Student{ Student s3("mno", "17-333333-1", "csse");
string name; Student::Student(string nm, string i, string dep)
string id; { s1.printStudentInfo();
string dept; name = nm; s2.printStudentInfo();
public: id = i; s3.printStudentInfo();
static string university; dept = dep;
//can access with the class name
} Student::university = "American International";
Student();
Student(string, string, string); void Student::printStudentInfo(){ s1.printStudentInfo();
void printStudentInfo(); cout << "Name: " << name << endl s2.printStudentInfo();
void changeUniversity(string newUni); << "ID: " << id << endl s3.printStudentInfo();
}; << "Dept: " << dept << endl
//can access by an object
<< "University: " << university s1.university = "American International University-Bang";
//initianizing static data member/variable << endl << "_______________” s1.printStudentInfo();
string Student::university = "AIUB"; << endl; s2.printStudentInfo();
} s3.printStudentInfo();

void Student::changeUniversity(string newUni) //can access through a non static member function
s2.changeUniversity("AIUB again");
{
university = newUni; s1.printStudentInfo();
} s2.printStudentInfo();
s3.printStudentInfo(); }
output

Name: xyz
ID: 17-111111-1 Name: xyz
Dept: cs ID: 17-111111-1
University: AIUB Dept: cs
_______________ University: American International University-Bangladesh
Name: abc _______________
ID: 17-222222-1 Name: abc
Dept: cse ID: 17-222222-1
University: AIUB Dept: cse
_______________ University: American International University-Bangladesh
Name: mno _______________
ID: 17-333333-1 Name: mno
Dept: csse ID: 17-333333-1
University: AIUB Dept: csse
_______________ University: American International University-Bangladesh
Name: xyz _______________
ID: 17-111111-1 Name: xyz
Dept: cs ID: 17-111111-1
University: American International Dept: cs
_______________ University: AIUB again
Name: abc _______________
ID: 17-222222-1 Name: abc
Dept: cse ID: 17-222222-1
University: American International Dept: cse
_______________ University: AIUB again
Name: mno _______________
ID: 17-333333-1 Name: mno
Dept: csse ID: 17-333333-1
University: American International Dept: csse
_______________ University: AIUB again
_______________
A PRIVATE STATIC MEMBER VARIABLE
EXAMPLE
#include <iostream>
using namespace std; int main(){
Student s1("xyz", "17-111111-1", "cs");
Student::Student(){} Student s2("abc", "17-222222-1", "cse");
class Student{ Student s3("mno", "17-333333-1", "csse");
string name; Student::Student(string nm, string i, string dep)
string id; { s1.printStudentInfo();
string dept; name = nm; s2.printStudentInfo();
static string university; id = i; s3.printStudentInfo();
public: dept = dep;
//cannot access with the class name
} Student::university = "American International";
Student();
Student(string, string, string); void Student::printStudentInfo(){ s1.printStudentInfo();
void printStudentInfo(); cout << "Name: " << name << endl s2.printStudentInfo();
void changeUniversity(string newUni); << "ID: " << id << endl s3.printStudentInfo();
}; << "Dept: " << dept << endl
//cannot access by an object
<< "University: " << university s1.university = "American International University-Bang";
//initianizing static data member/variable << endl << "_______________” s1.printStudentInfo();
string Student::university = "AIUB"; << endl; s2.printStudentInfo();
} s3.printStudentInfo();

void Student::changeUniversity(string newUni) //can access through a non static member function
s2.changeUniversity("AIUB again");
{
university = newUni; s1.printStudentInfo();
} s2.printStudentInfo();
s3.printStudentInfo(); }
STATIC MEMBER FUNCTION
STATIC MEMBER FUNCTION
¡ A static member function is associated with the class, not with the object.
¡ A static member function can be called even if no objects of the class exist.
¡ The static functions are accessed using only the class name and the scope resolution operator ::.
¡ A static member function can
¡ Only access static data member
¡ Only Other static member functions.
¡ A static member function cannot
¡ Access a non static data member.
¡ Access a non static member function.
¡ Static member functions do not have access to the this pointer of the class.
#include<iostream> int Item::count=0;
using namespace std;
Item::Item(string nm){ name = nm; count++;}
class Item{ string Item::getItemName(){return name;} int main(){
private: // can be accessed without an object
string name; //static function
cout << "count: " << Item::getItemCount()
static int count; int Item::getItemCount(){return count;} << endl;
public: //static function
Item(string); void Item::showcount(){
Item item1("Pencil");
string getItemName(); //cannot access non static member function
static int getItemCount(); cout<< "Item name: " << getItemName() << endl; // static member functions can be accessed
with object reference
static void showcount(); //cannot access non static variable
}; cout << "Item name: " << name << endl; cout<< "item1: " << item1.getItemName() <<
" count: " << item1.getItemCount();
//can access other static member function
cout<< "Item count: " << getItemCount() << endl; }
//can access static member variable
cout<< "Item count: " << count << endl;
}
QUICK TEST !!

ü Can we access a non static member function or variable from a static member function?

ü Can we access a static member function or variable from a non static member function?
PASSING OBJECT AS FUNCTION PARAMETER
WHAT IS THE OUTPUT

By default object are passed by value, and the copy constructor is called!

void changeTime(Time t)
class Time {
{ Time is: 3:10
int hr; int min; t.setTime(10);
Time is: 3:10
public: }

void setTime(int h=0,int m=0) {


int main(){
hr=h,min=m;
Time t1;
} t1.setTime(3,10);
void getTime() t1.getTime();

{
changeTime(t1);
cout<<"Time is: " << hr <<“:” <<min <<endl;
} t1.getTime();
}; return 0;
}
WHAT IS THE OUTPUT

Passing object by reference would solve the issue!

void changeTime(Time& t)
class Time {
{ Time is: 3:10
int hr; int min; t.setTime(10);
Time is: 10:0
public: }

void setTime(int h=0,int m=0) {


int main(){
hr=h,min=m;
Time t1;
} t1.setTime(3,10);
void getTime() t1.getTime();

{
changeTime(t1);
cout<<"Time is: " << hr <<“:” <<min <<endl;
} t1.getTime();
}; return 0;
}
SOLVING PUZZLE WITH DATA TYPES IN FUNCTION OVERLOADING
#include <iostream> int main()
#include <string>
using namespace std; {
float x=10, y=20;
long double multiply(long double x, long double y){
cout <<endl << "long double is called."; double a=20.3,b=30.2;
return x*y; cout << endl << multiply(x,y);
}
cout << endl << multiply(a,b);
double multiply(double x, double y){ cout << endl << multiply(10.2f, 4.2f);
cout <<endl << "double is called.";
return x*y; cout << endl << multiply(10.2, 4.2);
} cout << endl << multiply(10.2L, 4.2L);

float multiply(float x, float y){ }


cout<< endl << "float is called."; float is called.
return x*y; 200
} double is called.
613.06
float is called.
int multiply(int x, int y){ 42.84
cout<< endl << "int is called."; double is called.
42.84
return x*y; long double is called.
} 42.84
INTRODUCTION TO INHERITANCE
INHERITANCE - WHAT

Inheritance is one of the key features of Object-oriented programming in C++.

Inheritance: To create a new class (derived class) that is derived from an existing class (Base class) is called
inheritance.

The derived class inherits all / most of the features from the base class and can have additional features of its own.

It is also known as “is a”/”kind of”/”is a kind of” relationship.

Sub Class / Derived Class: The class that inherits properties from another class is called Sub class or Derived Class.

Super Class / Base Class: The class whose properties are inherited by sub class is called Base Class or Super class.
INHERITANCE – WHY (WITHOUT INHERITANCE)
Bus Car MotorCycle
_______ _______ _______
Wheels Wheels Wheels
Brakes Brakes Brakes
Engine Engine Engine
FuleCapacity FuleCapacity FuleCapacity
FuleLevel FuleLevel FuleLevel
_______ _______ _______
getFuelAmount() getFuelAmount() getFuelAmount()
applyBrakes() applyBrakes() applyBrakes()
setWheels() setWheels() setWheels()
setEngineConfig() setEngineConfig() setEngineConfig()
INHERITANCE – WHY (WITH INHERITANCE)
Bus LongDistanceBus
_____
NoOfSeats
Vehicle NoOfDoors
_______ ________ CityBus
setDoors()
Wheels
Brakes
Engine Car
FuleCapacity ________
Sedan
FuleLevel NoOfDoors
_______ ________
getFuelAmount() setDoors() HatchBack
applyBrakes()
setWheels()
setEngineConfig()
MotorCycle SportsBike
__________
bikeType
__________
setBikeType() NakedBike

Base Class Derived class


WHAT IS INHERITED
WHAT IS INHERITED BY DERIVED CLASS

There are three type of access modifiers:


1. Public
2. Protected
3. Private

§ A derived class inherits all the public variables and functions defined in the base class.

§ A derived class never inherits any private variables and functions defined in the base class.

But what about Protected????


WHAT IS INHERITED BY DERIVED CLASS

Protected access modifier in Inheritance:

§ Protected members are inherited.

§ The derived class can access and use protected member functions and variables.

§ However, direct access outside the class (by object name followed by a dot operator) is not

allowed.

§ In this regard private and protected members behaves exactly same.


INHERITANCE - EXAMPLE
#include <iostream> class Car: public Vehicle {
#include <string> int noOfDoors;
using namespace std; public:
Car(int doors){noOfDoors = doors;}
class Vehicle{ int getDoors (){return noOfDoors; }
string engine; };
double fuelLevel;
public: class Bike: public Vehicle {
void setFuleLevel(double level){fuelLevel = level;} string bikeType;
void setEngineConfig(string enginconf){engine = enginconf;} public:
double getFuelAmount(){return fuelLevel;} void setBikeType(string type){bikeType = type;}
string getBikeType(){ return bikeType; }
string getEngineConfig(){return engine;}
};
};

int main(){
Bike bike1;
bike1.setBikeType("racing");

bike1.setEngineConfig("1050cc");
bike1.setFuleLevel(50.6);

cout << "bike1 config: " << endl<< bike1.getBikeType() << endl<< bike1.getFuelAmount() << endl<< bike1.getEngineConfig();
}

You might also like