SlideShare a Scribd company logo
Object Oriented
Programming
Lecture #11
Separation of interface and
implementation
Public member function exposed by
a class is called interface
Separation of implementation from
the interface is good software
engineering
Complex Number
There are two representations of
complex number
 Euler form
z = x + i y
 Phasor form
z = |z| (cos  + i sin )
z is known as the complex modulus and
 is known as the complex argument or
phase
Example
float getX()
float getY()
void setNumber
(float i, float j)
…
float x
float y
Complex
Old implementation
float getX()
float getY()
void setNumber
(float i, float j)
…
float z
float theta
Complex
New
implementation
Example
class Complex{ //old
float x;
float y;
public:
void setNumber(float i, float j){
x = i;
y = j;
}
…
};
Example
class Complex{ //new
float z;
float theta;
public:
void setNumber(float i, float j){
theta = arctan(j/i);
…
}
…
};
Advantages
User is only concerned about ways
of accessing data (interface)
User has no concern about the
internal representation and
implementation of the class
Separation of interface and
implementation
Usually functions are defined in
implementation files (.cpp) while the
class definition is given in header file
(.h)
Some authors also consider this as
separation of interface and
implementation
Student.h
class Student{
int rollNo;
public:
void setRollNo(int aRollNo);
int getRollNo();
…
};
Student.cpp
#include “student.h”
void Student::setRollNo(int aNo){
…
}
int Student::getRollNo(){
…
}
Driver.cpp
#include “student.h”
int main(){
Student aStudent;
}
There are functions that are
meant to be read only
There must exist a mechanism
to detect error if such functions
accidentally change the data
member
const Member Functions
Keyword const is placed at
the end of the parameter list
const Member Functions
const Member Functions
Declaration:
class ClassName{
ReturnVal Function() const;
};
Definition:
ReturnVal ClassName::Function() const{
…
}
Example
class Student{
public:
int getRollNo() const {
return rollNo;
}
};
const Functions
Constant member functions cannot
modify the state of any object
They are just “read-only”
Errors due to typing are also
caught at compile time
Example
bool Student::isRollNo(int aNo){
if(rollNo = = aNo){
return true;
}
return false;
}
Example
bool Student::isRollNo(int aNo){
/*undetected typing mistake*/
if(rollNo = aNo){
return true;
}
return false;
}
Example
bool Student::isRollNo
(int aNo)const{
/*compiler error*/
if(rollNo = aNo){
return true;
}
return false;
}
const Functions
Constructors and Destructors
cannot be const
Constructor and destructor are
used to modify the object to a well
defined state
Example
class Time{
public:
Time() const {} //error…
~Time() const {} //error…
};
const Function
Constant member function
cannot change data member
Constant member function
cannot access non-constant
member functions
Example
class Student{
char * name;
public:
char *getName();
void setName(char * aName);
int ConstFunc() const{
name = getName();//error
setName(“Ahmad”);//error
}
};
this Pointer and const Member
Function
this pointer is passed as constant
pointer to const data in case of
constant member functions
const Student *const this;
instead of
Student * const this;
Problem
Change the class Student such
that a student is given a roll
number when the object is
created and cannot be changed
afterwards
Student Class
class Student{
…
int rollNo;
public:
Student(int aNo);
int getRollNo();
void setRollNo(int aNo);
…
};
Modified Student Class
class Student{
…
const int rollNo;
public:
Student(int aNo);
int getRollNo();
void setRollNo(int aNo);
…
};
Example
Student::Student(int aRollNo)
{
rollNo = aRollNo;
/*error: cannot modify a
constant data member*/
}
Example
void Student::SetRollNo(int i)
{
rollNo = i;
/*error: cannot modify a constant data member*/
}
Member Initializer List
A member initializer list is a
mechanism to initialize data members
It is given after closing parenthesis of
parameter list of constructor
In case of more then one member use
comma separated list
Example
class Student{
const int rollNo;
char *name;
float GPA;
public:
Student(int aRollNo)
: rollNo(aRollNo), name(Null), GPA(0.0){
…
}
…
};
Order of Initialization
 Data member are initialized in order they are declared
 Order in member initializer list is not significant at all
Example
class ABC{
int x;
int y;
int z;
public:
ABC();
};
Example
ABC::ABC():y(10),x(y),z(y)
{
…
}
/* x = Junk value
y = 10
z = 10 */
const Objects
Objects can be declared
constant with the use of const
keyword
Constant objects cannot
change their state
Example
int main()
{
const Student aStudent;
return 0;
}
Example
class Student{
…
int rollNo;
public:
…
int getRollNo(){
return rollNo;
}
};
Example
int main(){
const Student aStudent;
int a = aStudent.getRollNo();
//error
}
const Objects
const objects cannot access
“non const” member function
Chances of unintentional
modification are eliminated
Example
class Student{
…
int rollNo;
public:
…
int getRollNo()const{
return rollNo;
}
};
Example
int main(){
const Student aStudent;
int a = aStudent.getRollNo();
}
Constant data members
 Make all functions that don’t change the state of the object constant
 This will enable constant objects to access more member functions
Static Variables
Lifetime of static variable is
throughout the program life
If static variables are not explicitly
initialized then they are initialized
to 0 of appropriate type
Example
void func1(int i){
static int staticInt = i;
cout << staticInt << endl;
}
int main(){
func1(1);
func1(2);
}
Output:
1
1
Static Data Member
Definition
“A variable that is part of a
class, yet is not part of an
object of that class, is called
static data member”
Static Data Member
They are shared by all
instances of the class
They do not belong to any
particular instance of a class
Class vs. Instance Variable
Student s1, s2, s3;
Class Space
s1(rollNo,…)
s2(rollNo,…)
s3(rollNo,…)
Instance Variable
Class Variable
Static Data Member (Syntax)
Keyword static is used to make a
data member static
class ClassName{
…
static DataType VariableName;
};
Defining Static Data Member
Static data member is declared
inside the class
But they are defined outside the
class
Defining Static Data Member
class ClassName{
…
static DataType VariableName;
};
DataType ClassName::VariableName;
Initializing Static Data Member
Static data members should be
initialized once at file scope
They are initialized at the time
of definition
Example
class Student{
private:
static int noOfStudents;
public:
…
};
int Student::noOfStudents = 0;
/*private static member cannot be accessed
outside the class except for
initialization*/
Initializing Static Data Member
If static data members are not
explicitly initialized at the time
of definition then they are
initialized to 0
Example
int Student::noOfStudents;
is equivalent to
int Student::noOfStudents=0;
 Than You
 Questions

More Related Content

Similar to C++ Object Oriented Programming Lecture Slides for Students (20)

PPTX
OOP.pptx
saifnasir3
 
PPT
Unit vi(dsc++)
Durga Devi
 
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
nandemprasanna
 
PDF
Chapter 12 PJPK SDSDRFHVRCHVFHHVDRHVDRVHGVD
azimah6642
 
PPT
oop objects_classes
sidra tauseef
 
PPTX
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
PDF
Implementation of oop concept in c++
Swarup Boro
 
PPT
Class and object in C++
rprajat007
 
PPT
Classes in C++ computer language presentation.ppt
AjayLobo1
 
PDF
Implementation of oop concept in c++
Swarup Kumar Boro
 
PPTX
Data members and member functions
Marlom46
 
PPT
Savitch Ch 11
Terry Yoast
 
PPT
Savitch ch 11
Terry Yoast
 
PPT
static member and static member fumctions.ppt
poojitsaid2021
 
PDF
Object Oriented Programming Constructors & Destructors
anitashinde33
 
PPT
Classes, objects and methods
farhan amjad
 
PPTX
Object oriented design
lykado0dles
 
PPT
DS Unit 6.ppt
JITTAYASHWANTHREDDY
 
PPTX
Operator overload rr
Dhivya Shanmugam
 
PDF
Class object
Dr. Anand Bihari
 
OOP.pptx
saifnasir3
 
Unit vi(dsc++)
Durga Devi
 
cpp class unitdfdsfasadfsdASsASass 4.ppt
nandemprasanna
 
Chapter 12 PJPK SDSDRFHVRCHVFHHVDRHVDRVHGVD
azimah6642
 
oop objects_classes
sidra tauseef
 
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
Implementation of oop concept in c++
Swarup Boro
 
Class and object in C++
rprajat007
 
Classes in C++ computer language presentation.ppt
AjayLobo1
 
Implementation of oop concept in c++
Swarup Kumar Boro
 
Data members and member functions
Marlom46
 
Savitch Ch 11
Terry Yoast
 
Savitch ch 11
Terry Yoast
 
static member and static member fumctions.ppt
poojitsaid2021
 
Object Oriented Programming Constructors & Destructors
anitashinde33
 
Classes, objects and methods
farhan amjad
 
Object oriented design
lykado0dles
 
DS Unit 6.ppt
JITTAYASHWANTHREDDY
 
Operator overload rr
Dhivya Shanmugam
 
Class object
Dr. Anand Bihari
 

Recently uploaded (20)

PPTX
Practice Gardens and Polytechnic Education: Utilizing Nature in 1950s’ Hu...
Lajos Somogyvári
 
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
PDF
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
PDF
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
PPTX
Matatag Curriculum English 8-Week 1 Day 1-5.pptx
KirbieJaneGasta1
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PDF
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
PDF
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
DOCX
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PPTX
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
Practice Gardens and Polytechnic Education: Utilizing Nature in 1950s’ Hu...
Lajos Somogyvári
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
Matatag Curriculum English 8-Week 1 Day 1-5.pptx
KirbieJaneGasta1
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
Ad

C++ Object Oriented Programming Lecture Slides for Students