SlideShare a Scribd company logo
Amrit Kaur C++ as Object oriented Language
1
Chapter 7: C++ as Object Oriented Language
Contents
7.1 Classes in C++ ..............................................................................................................................1
7.1.1 Declaring a Class................................................................................................................... 1
7.1.2 Data Members...................................................................................................................... 2
7.1.3 Member Functions................................................................................................................ 2
7.2 Creating Objects in C++................................................................................................................3
7.3 Implementing Abstraction and Encapsulation using Access Specifiers..........................................5
7.3.1 public Access Specifier .......................................................................................................... 5
7.3.2 private Access Specifier......................................................................................................... 6
7.3.2 protected Access Specifier .................................................................................................... 7
7.4 Constructor .................................................................................................................................8
7.4.1 Need of Constructor.............................................................................................................. 9
7.4.2 Declaration of Constructor.................................................................................................... 9
7.4.3 Types of Constructor........................................................................................................... 10
7.4.4 Points to remember about constructor ............................................................................... 11
7.5 Destructors................................................................................................................................ 12
7.5.1 Need of Destructors............................................................................................................ 12
7.5.2 Declaration of Destructors .................................................................................................. 12
7.4.3 Points to remember about constructor ............................................................................... 13
7.1 Classes in C++
A class is set of similar object that exhibit some well defined behaviour. In C++, a class
is a user defined or abstract data type which holds both the data and function.
The state or attributes or data of a class is called member data. The functions or
behaviour of class are called member function. The variable or instance of a class is
called object.
7.1.1 Declaring a Class
A class keyword is used to declare a class. The braces are used to indicate start
and end of class body. A semicolon is used to end the class declaration. The syntax
of creating a class in C++ are
class <classname>
{ private:
data_type <data_member(s)>;
member_functions();
public:
data_type <data_member(s)>;
member_functions();
protected:
data_type <data_member(s)>;
Amrit Kaur C++ as Object oriented Language
2
member_functions();
};
Example 1 and Example 3 demonstrate how class can be declared
7.1.2 Data Members
Object have attributes or properties. In c++, attributes of a class are
represented using data members. All data members must be declared inside the
class not within any function body. The syntax of declaring a data member are
datatype datamembername;
Example 1 and Example 3 demonstrate how datamember of class can be declared
7.1.3 Member Functions
Object interacts with each other by passing messages and responding to them. In
OOP, object uses methods to pass messages. In C++, the task of message passing
can be done by member function. Member function operates on data member of
the class. Example 1 demonstrate how member function of class can be declared
Example 1: The following class declaration provides the behaviour speak() that display
the message ” I can speak Hindi and English”. The example also shows that every
person have similar attributes like name, age and gender.
Include<iostream.h>
class Person //person is a class
{ public:
//data members
char name[25];
int age;
char gender[5];
//member function
void speak()
{ cout<<” I can speak Hindi and English”;
}
};
In C++, function can be defined either
 Inside. The syntax of defining a member function inside the class are
return_type function_name(parameters)
{
Amrit Kaur C++ as Object oriented Language
3
statement(s);
}
As in example 1, the function speak() is defined inside the class.
 Outside. The scope resolution operator (::) is used to define the function
outside the class. The syntax of creating a function outside is as follows
return_type classname :: function_name(parameters)
{
statement(s);
}
When the function is defined outside the class, it is compulsory to have
function prototype within class.
Example 2: A code to declare member function outside the class.
class Person //person class
{ public:
//data members
char name[25];
int age;
char gender[5];
//member prototype
void speak();
};
//member function defined outside the class using (::)
void Person :: speak()
{ cout<<” I can speak Hindi and English”;
}
7.2 Creating Objects in C++
A class declaration doesn’t reserve any memory. Memory is allocated only when
an instance of a class is created. An instance of a class is called object.
The snytax of creating an object of a class are
classname objectname;
For Example :
Person p; //p is an object of class Person
Fruit mango;// Fruit is the name of class and mango is object
All the objects share same copy of member functions, but maintain a separate
copy of data members. All the member function and data members of a class are
accessed via object using dot operator(.).
Amrit Kaur C++ as Object oriented Language
4
The syntax of accessing data members are
objectname.datamembername;
The syntax of accessing member function are
objectname.memberfunctionname
For Example :
p.name=”John”; // name is data member, p is object of Class person
p.speak();// speak() is member function
Example 3: The following program illustrates the use of objects and classes in C++.
#include<conio.h>
#include<iostream.h>
class Person //person class
{ private:
//data members
char name[25];
int age;
char gender[5];
public:
//member function
void speak()
{ cout<<"nI can speak Hindi and English";
}
//member function
void input()
{
cout<<"n Enter Name";
cin>>name;
cout<<"n ENter Age";
cin>>age;
cout<<"n Enter Gender";
cin>>gender;
}
//member function
void display()
{
cout<<"n Name="<<name;
cout<<"n Age="<<age;
cout<<"n Gender="<<gender;
}
}; //class ends
void main()
{ clrscr();
Person p; //p is an object of class Person
Amrit Kaur C++ as Object oriented Language
5
cout<<"n Enter person Details";
p.input(); //call to all member function using dot operator
cout<<"n Display Person Details";
p.display(); //call to all member function using dot operator
p.speak(); //call to all member function using dot operator
}
7.3 Implementing Abstraction and Encapsulation using Access Specifiers
An access specifiers is used to determine whether any other class or function can
access the data member and member function of a particular class. In C++, there are
three access specifiers
 public
 private
 protected
7.3.1 public Access Specifier
The public data members and member function of a class can be accessed anywhere in
the application. An object defined outside the class for example in void main() can
only access public member function and data.
Example 4: Sample code showing use of public access specifier
#include<iostream.h>
class Calculator
{
public:
int a;
int b;
int c;
//member function
void input()
{
cout<<"Enter a=";
cin>>a;
cout<<"Enter b=";
cin>>b;
}
//member function
void add()
{
c=a+b;
cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c;
}
//member function
void display()
{
Amrit Kaur C++ as Object oriented Language
6
cout<<"n a="<<a<<" n b="<<b;
}
} ;
void main()
{
Calculator c; //object created
c.input(); //OK! public member function call
c.display(); // OK! public member function call
c.add(); // OK! public member function call
c.a=20; // OK! allowed...public data member
cout<<"n Value of a changed";
c.display();
c.add();
}
7.3.2 private Access Specifier
The private data member and member function of a class can only be accessed by the
member function of that class. Private members cannot be accessed outside the class
body. That is, private access specifier hides members of a particular class from other
classes and functions. Laws says that data must be private.
The default access specifier for a class in C++ is private. It means, if no other access
specifier is written in class body, all data members and member functions are private
by default.
Example 5: Sample code showing use of private access specifier
#include<conio.h>
#include<iostream.h>
class Calculator
{
private:
int a;
int b;
int c;
//member function
void input()
{
cout<<"Enter a=";
cin>>a;
cout<<"Enter b=";
cin>>b;
}
public:
//member function
void add()
{
c=a+b;
cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c;
}
Amrit Kaur C++ as Object oriented Language
7
void display()
{
cout<<"n a="<<a<<" n b="<<b;
}
} ;
void main()
{
Calculator c; //object created
c.input(); //ERROR! input() is private inaccessible outside class
c.display(); //OK! public member function call
c.add(); //Ok! public member function call
c.a=20; //ERROR! a is private inaccessible outside class
cout<<"n Value of a changed";
c.display();
c.add();
}
7.3.2 protected Access Specifier
Like private access specifier, protected access specifier also hides data member and
member function of a class from other classes and functions. But it is useful while
implementing inheritance because the protected data member and member function
of a class can only be accessed by the member function of that class and the member
function of class derived from it.
Example 6: Sample code showing use of protected access specifier
#include<iostream.h>
#include<conio.h>
class Calculator
{
protected:
int a;
int b;
int c;
//member function
void input()
{
cout<<"Enter a=";
cin>>a;
cout<<"Enter b=";
cin>>b;
}
public:
//member function
void add()
{
c=a+b;
cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c;
}
Amrit Kaur C++ as Object oriented Language
8
void display()
{
cout<<"n a="<<a<<" n b="<<b;
}
} ;
class StdCalculator: Calculator // subclass
{ float root;
public
void square()
{ //calling data member and member function of base class
input(); //OK! StdCalculator is subclass of Calculator
cout<<"nSquare of a"<<a*a; //OK! StdCalculator is subclass of Calculator
cout<<"nSquare of b"<<b*b; //OK! StdCalculator is subclass of Calculator
}
};
void main()
{
clrscr();
Calculator c; //object created
c.input; //ERROR! it is protected. inaccessible outside class accessible within class and in subclass
c.display();
c.add();
c.a=20; //ERROR! a is protected. inaccessible outside class accessible within class and in subclass
cout<<"n Value of a changed";
c.display();
c.add();
StdCalculator s;
s.square();
}
The table shows visibility of class members for all three access specifiers
Access
Specifier
Visible to own class members Visible to sub class Visible to outside class
public Yes Yes Yes
private Yes No No
Protected Yes Yes No
7.4 Constructor
Constructors are the default member function of a class that are invoked automatically
when an object of class is created. They are the special function that have same name
as that of class name.
Amrit Kaur C++ as Object oriented Language
9
7.4.1 Need of Constructor
Every time object is created, memory is allocated to it but this doesn’t initialize
data members of a class. Further, in C++, we cannot initialize data member of a
class while declaring it.
class Calculator
{ //data members
int a=0; //ERROR cannot initialize class member here
int b=0; //ERROR cannot initialize class member here
int c=0; //ERROR cannot initialize class member here
…..
}
Thus there is a need of function that assign initial value to data members. C++
provides solution to this problem. C++ allows automatic initialization of data
members by using constructor functions as and when object is created.
7.4.2 Declaration of Constructor
Constructors have same as that of class name. The declaration is same as other
member function of class except that constructor doesn’t return any value. The
syntax is as follow
classname (parameter(s)
{
// data members initialisation
}
Example 7 : Calculator class illustrate constructor.
#include<iostream.h>
class Calculator
{ //data members
int a;
int b;
int c;
public:
//constructor
Calculator()
{
a=b=c=0;
cout<<"Object Created. n Constructor Called automatically";
}
//member function
void input()
{
Amrit Kaur C++ as Object oriented Language
10
cout<<"Enter a=";
cin>>a;
cout<<"Enter b=";
cin>>b;
}
//member function
void add()
{
c=a+b;
cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c;
}
} ;
void main()
{
Calculator c; //object created; constructor call automatically
c.input(); //member function call
c.add(); //member function call
}
7.4.3 Types of Constructor
There are two types of Constructor
A. Default Constructor: A constructor that doesnot have any parameter is
known as default constructor.
B. Parameterised Constructor: Constructor can be modified to accept the user
supplied values at run time. This is done by defining constructors with
parameters known as parameterised constructor. It means, a constructor
can be overloaded.
Example 8: Sample coding showing parameterised constructor
#include<iostream.h>
class Calculator
{
int a;
int b;
int c;
public:
// default constructor – no parameter
Calculator()
{
a=b=c=0;
cout<<"Object Created. n Constructor Called Automatically";
}
//parameterised constructor
Amrit Kaur C++ as Object oriented Language
11
Calculator(int p)
{
a=b=c=0;
cout<<"Object Created. Parameterised Constructor Called Automatically";
}
//member function
void input()
{
cout<<"Enter a=";
cin>>a;
cout<<"Enter b=";
cin>>b;
}
//member function
void add()
{
c=a+b;
cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c;
}
} ;
void main()
{
Calculator c;//object created
c.input(); //member function call
c.add(); //member function call
Calculator p(10); //object created , call to parameterised
p.add();
}
7.4.4 Points to remember about constructor
 Constructors are default member function of a class that are called
automatically when an object of class is created.
 Constructor cannot be called explicitly.
 Constructors have same name as that of class name.
 Constructors are used to initialise data members of a class.
 Constructor have no return type
 Constructors cannot be declared private.
 Constructors can be overload. That is, a class can have any number of
constructors.
Amrit Kaur C++ as Object oriented Language
12
7.5 Destructors
Destructors function are the member function of a class that are invoked automatically
when an object goes out of scope.
7.5.1 Need of Destructors
Destructors are the special functions that are used to de-initialise the objects when
they are destroyed. Therefore, programme is relieved of the task of clearing memory
space occupied by data members when object cease to exist.
7.5.2 Declaration of Destructors
Destructors have same as that of class name except that it is prefixed with a tilde
(¬) . It doesn’t have parameters and return any value. The syntax is as follow
¬classname()
{
// data members initialisation
}
Example 9: Sample coding showing destructor
#include<iostream.h>
class Calculator
{
int a;
int b;
int c;
public:
//constructor
Calculator()
{
a=b=c=0;
cout<<"Object Created. n Constructor Called Automatically";
}
//Destructor
~Calculator()
{
cout<<"Destructor called.... Object Goes out of Scope";
}
//member function
void input()
{
cout<<"Enter a=";
cin>>a;
Amrit Kaur C++ as Object oriented Language
13
cout<<"Enter b=";
cin>>b;
}
//member function
void add()
{
c=a+b;
cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c;
}
} ;
void main()
{
Calculator c;//object created
c.input(); //member function call
c.add(); //member function call
}
7.4.3 Points to remember about constructor
 Destructors are the special member function of a class that are called
automatically when an object of class goes out of scope
 Destructors can be called explicitly but not recommended.
Example: Objectname.¬classname();
 Destructors have same name as that of class name but prefixed with
tilde(¬).
 Destructors are used to de- initialise data members of a class.
 Destructors have no return type and no parameters
 Destructors cannot be declared private.
 Destructors cannot be overload. That is, a class can have ONLY one
destructor.

More Related Content

What's hot (19)

DOCX
I assignmnt(oops)
Jay Patel
 
PDF
C++ Multiple Inheritance
harshaltambe
 
PPTX
ICT C++
Karthikeyan A K
 
PPT
Qno 2 (a)
Praveen M Jigajinni
 
PPTX
Java 2
Michael Shrove
 
PPTX
C#.net evolution part 2
Vahid Farahmandian
 
PDF
Back to the Future with TypeScript
Aleš Najmann
 
PPT
Understanding Reflection
Tamir Khason
 
PDF
OOP in PHP
Tarek Mahmud Apu
 
DOC
Introduction to classes the concept of a class/tutorialoutlet
Oldingz
 
PPTX
C#.net Evolution part 1
Vahid Farahmandian
 
PDF
LEARN C#
adroitinfogen
 
PPTX
Web2Day 2017 - Concilier DomainDriveDesign et API REST
Nicolas Faugout
 
PDF
Module wise format oops questions
SANTOSH RATH
 
PPTX
Understanding Interfaces
Bhushan Mulmule
 
DOC
Visual c sharp
Palm Palm Nguyễn
 
PDF
Programming C Part 01
Raselmondalmehedi
 
I assignmnt(oops)
Jay Patel
 
C++ Multiple Inheritance
harshaltambe
 
ICT C++
Karthikeyan A K
 
C#.net evolution part 2
Vahid Farahmandian
 
Back to the Future with TypeScript
Aleš Najmann
 
Understanding Reflection
Tamir Khason
 
OOP in PHP
Tarek Mahmud Apu
 
Introduction to classes the concept of a class/tutorialoutlet
Oldingz
 
C#.net Evolution part 1
Vahid Farahmandian
 
LEARN C#
adroitinfogen
 
Web2Day 2017 - Concilier DomainDriveDesign et API REST
Nicolas Faugout
 
Module wise format oops questions
SANTOSH RATH
 
Understanding Interfaces
Bhushan Mulmule
 
Visual c sharp
Palm Palm Nguyễn
 
Programming C Part 01
Raselmondalmehedi
 

Viewers also liked (10)

PPTX
Deber2
CHARLES ROMAN
 
PDF
OTDK angol
M J
 
PDF
When should you remodel your bathroom
jkoonerrenovations
 
PPT
Coaching For Effectiveness
Scontrino-Powell
 
DOCX
English basic
Stephen Jordison
 
PDF
Rotations poster
Antoine Taly
 
PPT
autism wheel power point
Dr Pete Marcelo
 
PDF
Ratificacions - ufcb
FC Barcelona
 
PPTX
Types of computer
Rajkumar Rajak
 
DOCX
Seven Eleven Store - Case study - Answers
Zaka Ul Hassan
 
OTDK angol
M J
 
When should you remodel your bathroom
jkoonerrenovations
 
Coaching For Effectiveness
Scontrino-Powell
 
English basic
Stephen Jordison
 
Rotations poster
Antoine Taly
 
autism wheel power point
Dr Pete Marcelo
 
Ratificacions - ufcb
FC Barcelona
 
Types of computer
Rajkumar Rajak
 
Seven Eleven Store - Case study - Answers
Zaka Ul Hassan
 
Ad

Similar to Chapter 7 C++ As OOP (20)

PPTX
class c++
vinay chauhan
 
PPT
Classes and objects
Lovely Professional University
 
PDF
Class and object
Prof. Dr. K. Adisesha
 
PDF
Object Oriented Programming using C++ - Part 2
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
PPTX
Lecture 4. mte 407
rumanatasnim415
 
PPTX
Presentation on class and object in Object Oriented programming.
Enam Khan
 
PPT
classes & objects.ppt
BArulmozhi
 
PDF
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
PDF
oopm 2.pdf
jayeshsoni49
 
PDF
C++ Notes
MOHAMED RIYAZUDEEN
 
PDF
Implementation of oop concept in c++
Swarup Boro
 
PPTX
Classes and objects
Anil Kumar
 
PPTX
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
vmickey4522
 
PPTX
Class and object
prabhat kumar
 
PPTX
05 Object Oriented Concept Presentation.pptx
ToranSahu18
 
PPTX
Classes and objects
Shailendra Veeru
 
PPT
C++ Programming Course
Dennis Chang
 
PPT
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
PPTX
3.Syntax.pptx for oops programing language
sanketkashyap2023
 
PPTX
Lecture 2 (1)
zahid khan
 
class c++
vinay chauhan
 
Classes and objects
Lovely Professional University
 
Class and object
Prof. Dr. K. Adisesha
 
Object Oriented Programming using C++ - Part 2
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Lecture 4. mte 407
rumanatasnim415
 
Presentation on class and object in Object Oriented programming.
Enam Khan
 
classes & objects.ppt
BArulmozhi
 
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
oopm 2.pdf
jayeshsoni49
 
Implementation of oop concept in c++
Swarup Boro
 
Classes and objects
Anil Kumar
 
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
vmickey4522
 
Class and object
prabhat kumar
 
05 Object Oriented Concept Presentation.pptx
ToranSahu18
 
Classes and objects
Shailendra Veeru
 
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
3.Syntax.pptx for oops programing language
sanketkashyap2023
 
Lecture 2 (1)
zahid khan
 
Ad

More from Amrit Kaur (20)

PDF
File Organization
Amrit Kaur
 
PDF
Introduction to transaction processing
Amrit Kaur
 
PDF
ER diagram
Amrit Kaur
 
PPTX
Transaction Processing
Amrit Kaur
 
PDF
Normalization
Amrit Kaur
 
PDF
Sample Interview Question
Amrit Kaur
 
PPTX
12. oracle database architecture
Amrit Kaur
 
PPTX
11. using regular expressions with oracle database
Amrit Kaur
 
PPTX
10. timestamp
Amrit Kaur
 
PPTX
9. index and index organized table
Amrit Kaur
 
PPTX
8. transactions
Amrit Kaur
 
PPTX
7. exceptions handling in pl
Amrit Kaur
 
PPTX
6. triggers
Amrit Kaur
 
PPTX
5. stored procedure and functions
Amrit Kaur
 
PPTX
4. plsql
Amrit Kaur
 
PPTX
3. ddl create
Amrit Kaur
 
PPTX
2. DML_INSERT_DELETE_UPDATE
Amrit Kaur
 
PPTX
1. dml select statement reterive data
Amrit Kaur
 
PDF
Chapter 6 OOPS Concept
Amrit Kaur
 
PDF
ComputerBasics
Amrit Kaur
 
File Organization
Amrit Kaur
 
Introduction to transaction processing
Amrit Kaur
 
ER diagram
Amrit Kaur
 
Transaction Processing
Amrit Kaur
 
Normalization
Amrit Kaur
 
Sample Interview Question
Amrit Kaur
 
12. oracle database architecture
Amrit Kaur
 
11. using regular expressions with oracle database
Amrit Kaur
 
10. timestamp
Amrit Kaur
 
9. index and index organized table
Amrit Kaur
 
8. transactions
Amrit Kaur
 
7. exceptions handling in pl
Amrit Kaur
 
6. triggers
Amrit Kaur
 
5. stored procedure and functions
Amrit Kaur
 
4. plsql
Amrit Kaur
 
3. ddl create
Amrit Kaur
 
2. DML_INSERT_DELETE_UPDATE
Amrit Kaur
 
1. dml select statement reterive data
Amrit Kaur
 
Chapter 6 OOPS Concept
Amrit Kaur
 
ComputerBasics
Amrit Kaur
 

Chapter 7 C++ As OOP

  • 1. Amrit Kaur C++ as Object oriented Language 1 Chapter 7: C++ as Object Oriented Language Contents 7.1 Classes in C++ ..............................................................................................................................1 7.1.1 Declaring a Class................................................................................................................... 1 7.1.2 Data Members...................................................................................................................... 2 7.1.3 Member Functions................................................................................................................ 2 7.2 Creating Objects in C++................................................................................................................3 7.3 Implementing Abstraction and Encapsulation using Access Specifiers..........................................5 7.3.1 public Access Specifier .......................................................................................................... 5 7.3.2 private Access Specifier......................................................................................................... 6 7.3.2 protected Access Specifier .................................................................................................... 7 7.4 Constructor .................................................................................................................................8 7.4.1 Need of Constructor.............................................................................................................. 9 7.4.2 Declaration of Constructor.................................................................................................... 9 7.4.3 Types of Constructor........................................................................................................... 10 7.4.4 Points to remember about constructor ............................................................................... 11 7.5 Destructors................................................................................................................................ 12 7.5.1 Need of Destructors............................................................................................................ 12 7.5.2 Declaration of Destructors .................................................................................................. 12 7.4.3 Points to remember about constructor ............................................................................... 13 7.1 Classes in C++ A class is set of similar object that exhibit some well defined behaviour. In C++, a class is a user defined or abstract data type which holds both the data and function. The state or attributes or data of a class is called member data. The functions or behaviour of class are called member function. The variable or instance of a class is called object. 7.1.1 Declaring a Class A class keyword is used to declare a class. The braces are used to indicate start and end of class body. A semicolon is used to end the class declaration. The syntax of creating a class in C++ are class <classname> { private: data_type <data_member(s)>; member_functions(); public: data_type <data_member(s)>; member_functions(); protected: data_type <data_member(s)>;
  • 2. Amrit Kaur C++ as Object oriented Language 2 member_functions(); }; Example 1 and Example 3 demonstrate how class can be declared 7.1.2 Data Members Object have attributes or properties. In c++, attributes of a class are represented using data members. All data members must be declared inside the class not within any function body. The syntax of declaring a data member are datatype datamembername; Example 1 and Example 3 demonstrate how datamember of class can be declared 7.1.3 Member Functions Object interacts with each other by passing messages and responding to them. In OOP, object uses methods to pass messages. In C++, the task of message passing can be done by member function. Member function operates on data member of the class. Example 1 demonstrate how member function of class can be declared Example 1: The following class declaration provides the behaviour speak() that display the message ” I can speak Hindi and English”. The example also shows that every person have similar attributes like name, age and gender. Include<iostream.h> class Person //person is a class { public: //data members char name[25]; int age; char gender[5]; //member function void speak() { cout<<” I can speak Hindi and English”; } }; In C++, function can be defined either  Inside. The syntax of defining a member function inside the class are return_type function_name(parameters) {
  • 3. Amrit Kaur C++ as Object oriented Language 3 statement(s); } As in example 1, the function speak() is defined inside the class.  Outside. The scope resolution operator (::) is used to define the function outside the class. The syntax of creating a function outside is as follows return_type classname :: function_name(parameters) { statement(s); } When the function is defined outside the class, it is compulsory to have function prototype within class. Example 2: A code to declare member function outside the class. class Person //person class { public: //data members char name[25]; int age; char gender[5]; //member prototype void speak(); }; //member function defined outside the class using (::) void Person :: speak() { cout<<” I can speak Hindi and English”; } 7.2 Creating Objects in C++ A class declaration doesn’t reserve any memory. Memory is allocated only when an instance of a class is created. An instance of a class is called object. The snytax of creating an object of a class are classname objectname; For Example : Person p; //p is an object of class Person Fruit mango;// Fruit is the name of class and mango is object All the objects share same copy of member functions, but maintain a separate copy of data members. All the member function and data members of a class are accessed via object using dot operator(.).
  • 4. Amrit Kaur C++ as Object oriented Language 4 The syntax of accessing data members are objectname.datamembername; The syntax of accessing member function are objectname.memberfunctionname For Example : p.name=”John”; // name is data member, p is object of Class person p.speak();// speak() is member function Example 3: The following program illustrates the use of objects and classes in C++. #include<conio.h> #include<iostream.h> class Person //person class { private: //data members char name[25]; int age; char gender[5]; public: //member function void speak() { cout<<"nI can speak Hindi and English"; } //member function void input() { cout<<"n Enter Name"; cin>>name; cout<<"n ENter Age"; cin>>age; cout<<"n Enter Gender"; cin>>gender; } //member function void display() { cout<<"n Name="<<name; cout<<"n Age="<<age; cout<<"n Gender="<<gender; } }; //class ends void main() { clrscr(); Person p; //p is an object of class Person
  • 5. Amrit Kaur C++ as Object oriented Language 5 cout<<"n Enter person Details"; p.input(); //call to all member function using dot operator cout<<"n Display Person Details"; p.display(); //call to all member function using dot operator p.speak(); //call to all member function using dot operator } 7.3 Implementing Abstraction and Encapsulation using Access Specifiers An access specifiers is used to determine whether any other class or function can access the data member and member function of a particular class. In C++, there are three access specifiers  public  private  protected 7.3.1 public Access Specifier The public data members and member function of a class can be accessed anywhere in the application. An object defined outside the class for example in void main() can only access public member function and data. Example 4: Sample code showing use of public access specifier #include<iostream.h> class Calculator { public: int a; int b; int c; //member function void input() { cout<<"Enter a="; cin>>a; cout<<"Enter b="; cin>>b; } //member function void add() { c=a+b; cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c; } //member function void display() {
  • 6. Amrit Kaur C++ as Object oriented Language 6 cout<<"n a="<<a<<" n b="<<b; } } ; void main() { Calculator c; //object created c.input(); //OK! public member function call c.display(); // OK! public member function call c.add(); // OK! public member function call c.a=20; // OK! allowed...public data member cout<<"n Value of a changed"; c.display(); c.add(); } 7.3.2 private Access Specifier The private data member and member function of a class can only be accessed by the member function of that class. Private members cannot be accessed outside the class body. That is, private access specifier hides members of a particular class from other classes and functions. Laws says that data must be private. The default access specifier for a class in C++ is private. It means, if no other access specifier is written in class body, all data members and member functions are private by default. Example 5: Sample code showing use of private access specifier #include<conio.h> #include<iostream.h> class Calculator { private: int a; int b; int c; //member function void input() { cout<<"Enter a="; cin>>a; cout<<"Enter b="; cin>>b; } public: //member function void add() { c=a+b; cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c; }
  • 7. Amrit Kaur C++ as Object oriented Language 7 void display() { cout<<"n a="<<a<<" n b="<<b; } } ; void main() { Calculator c; //object created c.input(); //ERROR! input() is private inaccessible outside class c.display(); //OK! public member function call c.add(); //Ok! public member function call c.a=20; //ERROR! a is private inaccessible outside class cout<<"n Value of a changed"; c.display(); c.add(); } 7.3.2 protected Access Specifier Like private access specifier, protected access specifier also hides data member and member function of a class from other classes and functions. But it is useful while implementing inheritance because the protected data member and member function of a class can only be accessed by the member function of that class and the member function of class derived from it. Example 6: Sample code showing use of protected access specifier #include<iostream.h> #include<conio.h> class Calculator { protected: int a; int b; int c; //member function void input() { cout<<"Enter a="; cin>>a; cout<<"Enter b="; cin>>b; } public: //member function void add() { c=a+b; cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c; }
  • 8. Amrit Kaur C++ as Object oriented Language 8 void display() { cout<<"n a="<<a<<" n b="<<b; } } ; class StdCalculator: Calculator // subclass { float root; public void square() { //calling data member and member function of base class input(); //OK! StdCalculator is subclass of Calculator cout<<"nSquare of a"<<a*a; //OK! StdCalculator is subclass of Calculator cout<<"nSquare of b"<<b*b; //OK! StdCalculator is subclass of Calculator } }; void main() { clrscr(); Calculator c; //object created c.input; //ERROR! it is protected. inaccessible outside class accessible within class and in subclass c.display(); c.add(); c.a=20; //ERROR! a is protected. inaccessible outside class accessible within class and in subclass cout<<"n Value of a changed"; c.display(); c.add(); StdCalculator s; s.square(); } The table shows visibility of class members for all three access specifiers Access Specifier Visible to own class members Visible to sub class Visible to outside class public Yes Yes Yes private Yes No No Protected Yes Yes No 7.4 Constructor Constructors are the default member function of a class that are invoked automatically when an object of class is created. They are the special function that have same name as that of class name.
  • 9. Amrit Kaur C++ as Object oriented Language 9 7.4.1 Need of Constructor Every time object is created, memory is allocated to it but this doesn’t initialize data members of a class. Further, in C++, we cannot initialize data member of a class while declaring it. class Calculator { //data members int a=0; //ERROR cannot initialize class member here int b=0; //ERROR cannot initialize class member here int c=0; //ERROR cannot initialize class member here ….. } Thus there is a need of function that assign initial value to data members. C++ provides solution to this problem. C++ allows automatic initialization of data members by using constructor functions as and when object is created. 7.4.2 Declaration of Constructor Constructors have same as that of class name. The declaration is same as other member function of class except that constructor doesn’t return any value. The syntax is as follow classname (parameter(s) { // data members initialisation } Example 7 : Calculator class illustrate constructor. #include<iostream.h> class Calculator { //data members int a; int b; int c; public: //constructor Calculator() { a=b=c=0; cout<<"Object Created. n Constructor Called automatically"; } //member function void input() {
  • 10. Amrit Kaur C++ as Object oriented Language 10 cout<<"Enter a="; cin>>a; cout<<"Enter b="; cin>>b; } //member function void add() { c=a+b; cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c; } } ; void main() { Calculator c; //object created; constructor call automatically c.input(); //member function call c.add(); //member function call } 7.4.3 Types of Constructor There are two types of Constructor A. Default Constructor: A constructor that doesnot have any parameter is known as default constructor. B. Parameterised Constructor: Constructor can be modified to accept the user supplied values at run time. This is done by defining constructors with parameters known as parameterised constructor. It means, a constructor can be overloaded. Example 8: Sample coding showing parameterised constructor #include<iostream.h> class Calculator { int a; int b; int c; public: // default constructor – no parameter Calculator() { a=b=c=0; cout<<"Object Created. n Constructor Called Automatically"; } //parameterised constructor
  • 11. Amrit Kaur C++ as Object oriented Language 11 Calculator(int p) { a=b=c=0; cout<<"Object Created. Parameterised Constructor Called Automatically"; } //member function void input() { cout<<"Enter a="; cin>>a; cout<<"Enter b="; cin>>b; } //member function void add() { c=a+b; cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c; } } ; void main() { Calculator c;//object created c.input(); //member function call c.add(); //member function call Calculator p(10); //object created , call to parameterised p.add(); } 7.4.4 Points to remember about constructor  Constructors are default member function of a class that are called automatically when an object of class is created.  Constructor cannot be called explicitly.  Constructors have same name as that of class name.  Constructors are used to initialise data members of a class.  Constructor have no return type  Constructors cannot be declared private.  Constructors can be overload. That is, a class can have any number of constructors.
  • 12. Amrit Kaur C++ as Object oriented Language 12 7.5 Destructors Destructors function are the member function of a class that are invoked automatically when an object goes out of scope. 7.5.1 Need of Destructors Destructors are the special functions that are used to de-initialise the objects when they are destroyed. Therefore, programme is relieved of the task of clearing memory space occupied by data members when object cease to exist. 7.5.2 Declaration of Destructors Destructors have same as that of class name except that it is prefixed with a tilde (¬) . It doesn’t have parameters and return any value. The syntax is as follow ¬classname() { // data members initialisation } Example 9: Sample coding showing destructor #include<iostream.h> class Calculator { int a; int b; int c; public: //constructor Calculator() { a=b=c=0; cout<<"Object Created. n Constructor Called Automatically"; } //Destructor ~Calculator() { cout<<"Destructor called.... Object Goes out of Scope"; } //member function void input() { cout<<"Enter a="; cin>>a;
  • 13. Amrit Kaur C++ as Object oriented Language 13 cout<<"Enter b="; cin>>b; } //member function void add() { c=a+b; cout<<endl<<"Sum of "<<a<<" and "<<b<<" is "<<c; } } ; void main() { Calculator c;//object created c.input(); //member function call c.add(); //member function call } 7.4.3 Points to remember about constructor  Destructors are the special member function of a class that are called automatically when an object of class goes out of scope  Destructors can be called explicitly but not recommended. Example: Objectname.¬classname();  Destructors have same name as that of class name but prefixed with tilde(¬).  Destructors are used to de- initialise data members of a class.  Destructors have no return type and no parameters  Destructors cannot be declared private.  Destructors cannot be overload. That is, a class can have ONLY one destructor.