SlideShare a Scribd company logo
CLASSES AND OBJECTS
ANIL KUMAR
https://www.facebook.com/AniLK0221
INTRODUCTION
• C++ class mechanism allows users to define
their own data types that can be used as
conveniently as build in types.
• Classes are often called user defined types.
• Includes defining of class types and creation of
objects of classes.
Cont…………
• The class definition introduces both :
• the class data members that define the
internal representation of class
• The class member functions that define the
set of operations that may be applied to
objects of class type.
Cont………….
• Information hiding is achieved by declaring
the data members as private whereas the
operations to be performed on class objects
by the program are public.
What is class?
• Created using keyword class.
• A class declaration defines a new user defined
data type that links code and data.
• The new data type is then used to declare
objects of that class.
• Class is a logical abstraction but an object has
physical existence.
• Objects is an instance of class.
Cont……….
• The class is the cornerstone of C++
– It makes possible encapsulation, data hiding and
inheritance
– A user defined type
– Consists of both data and methods
– Defines properties and behavior of that type
Syntax of Class declaration
• Class className
• {
• // body of a class
• }
Cont…………….
• The class definition has two parts:
• Class head : composed of keyword class
followed by the class name
• Class body: enclosed by a pair of curly braces.
General form of class declaration
• Class className
• {
• Access_specifier_1:
• Members;
• Access_specifier_2:
• Members;
• ……
• }
• objectName;
Cont…………
• The objectName is optional.
• If present, it declares objects of the class.
Example of student class
• Class student
• {
• Int rollNo; (data members)
• Char name [25];
• Char addr [35];
• Public:
• Void getdata(); (member functions)
• Void display();
• }
• ;
Cont………
• The class data members and member
functions are declared within the body of the
class along with their access levels.
• The class body defined the class member list
and their scope.
• If two classes have members with the same
name, the program will not show any error
since the members refer to different objects.
Access specifier
• Public
• Private
• Protected
Public
• Public members may be accessed by member
functions of same class and functions outside
the scope of the class (anywhere inside the
program)
Private
• Private members may only be accessed by
member functions and friend function of the
class.
• A class that enforces information hiding
declares its data members as private.
Protected
• The protected members may be accessed only
by the member functions of its class or by
member functions of its derived class.
Cont………..
• The data hiding is achieved by declaring data
members in the private section of the class.
• Since the private members are not accessible
from outside the class, they will be accessed
by the publicly declared member functions of
the same class.
Defining a class
class box
{
Public:
Int a,b,c; Data Members
void get()
{
cin>>a>>b>>c;
}
void put() Member Functions
{
cout<<a<<b<<c;
}
};
C++
IT 3rd Sem
Object-Oriented Programming:
Class:
class <class-name>
{
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function declarations
};
Example
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• Void setvalues (int, int);
• Int area();
• };
Cont………..
• It declares a class rectangle. The class contains
four members:
• Two data members length and width of type
integer in the private section (because private
section is default permission)
• Two member functions:
• Setvalues (int, int) and area() in the public
section.
C++
IT 3rd Sem
Object-Oriented Programming:
Object:
class <class-name>
{
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function declarations
} ob1; //object creation
void main()
{
<class-name> ob2; //object
creation
Objects
A class provides the blueprints for objects, so basically an
object is created from a class. Object can be called as an
instance of a class. We declare objects of a class with exactly
the same sort of declaration that we declare variables of basic
types.
Following statements declare two objects of class Box:
Box Box1;
Box Box2;
Cont………..
• The definition of class does not cause any
storage to be allocated. Storage is only
allocated when object of class type is defined.
• The process of creating objects of the class is
called class instantiation.
Syntax of defining objects of a class
• Class className objectName
• Class : keyword
• ClassName : user defined class name
• User defined object name
Object Operations
All operations that can be applied to basic data types can be
applied to the objects.
E.g.:
• Arithematic
• Relational
• Logical
Example
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• // member functions
• };
• The definition
• Rectangle rect;
Cont………..
Will allocate the memory space sufficient to
contain the two data members of the rectangle
class.
The name rect refers to that memory location.
Each class object has its own copy of the class
data members.
• An object of a class type also has a lifetime.
Data members
• Declared in the same way as the variables are
declared.
• Generally, all data members of a class are
made private to that class.
• If data members are declared in the public
section of the class, they will be accessed
using member access operator, dot (.).
Syntax of accessing data members of
class
• objectName . Data member
• ObjectName : user defined object name
• . : member access operator
• Data member: data member of a class.
Member functions
• Functions that are declared within a class are
called member functions.
• Works on data members of class.
• Member functions can access any element of
the class of which they belong to.
• The member functions of a class are declared
inside the class body.
Syntax of accessing member functions
of a class
• objectName . functionName (Actual
Arguments)
• objectName: user defined object name
• . : member access operator
• functionName: name of the member function
• Actual arguments: arguments list to the
function
Cont…………
• Member functions are declared within the
scope of their class. It means member
function is not visible outside the scope of its
class.
Member function can be defined in
two ways
• Inside the class
• Outside the class
Functions defined inside the class
• Member function of a class can be defined
inside the class declaration.
• Its syntax is similar to a normal function
definition except that it is enclosed within the
body of a class.
Example
• The member function in the rectangle class can be defined as follows:
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• Void setvalues(int x, int y)
• {
• Length = x;
• Width = y;
• }
• Int area ()
• {
• return(length * width);
• }
• } ;
Cont…….
• In this the class contains two member
functions in the public section:
Setvalues(int, int)
Area()
• They are defined inside the class itself.
Functions defined outside the class
• In this method, the prototype of the member
function is declared within the body of the
class and then defined outside the body of the
class.
• Functions defined outside the class have the
same syntax as the normal function, there
should be a mechanism of binding to the class
to which they belong.
Cont……………
• This requires a special declaration.
• The name of the member function must be
qualified by the name of its class by using the
scope resolution operator. ( : : )
General format of member function
definition
• Class className
• {
• ……
• returnType memberFunction (arguments);
• User defined class name
• }
• ;
Member function definition outside
the class
• returnType className :: memberFunction
(arguments)
• {
• // body of the function
• }
Object-Oriented Programming
Using C++, Third Edition
42
Implementing Class Functions
• When you construct a class, you create two parts:
– Declaration section: contains the class name,
variables (attributes), and function prototypes
– Implementation section: contains the functions
• Use both the class name and the scope resolution
operator (::) when you implement a class function
Object-Oriented Programming
Using C++, Third Edition
43
Implementing Class Functions
(continued)
Object-Oriented Programming
Using C++, Third Edition
44
Using Static Class Members
• When a class field is static, only one memory
location is allocated
– All members of the class share a single storage
location for a static data member of that same class
• When you create a non-static variable within a
function, a new variable is created every time you
call that function
• When you create a static variable, the variable
maintains its memory address and previous value
for the life of the program
Static variable
• Static variables are sometimes called class
variables, class fields, or class-wide fields
because they don’t belong to a specific object;
they belong to the class.
• Initialized and allocated storage only once at
the beginning of the program execution.
• No matters how many times they are called
and used in the program.
• Retains its value until the end of the program.
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 46
© 2006 Pearson Education.
All Rights Reserved
Static Members
• Static variables:
- Shared by all objects of the class
- Like a “global variable” among objects of the
class
• Static member functions:
- Can be used to access static member
variables
- Can be called before any objects are
created
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 47
© 2006 Pearson Education.
All Rights Reserved
Constant Member Functions
• Declared with keyword const
• When const follows the parameter list,
int getX() const; (in the class definition)
int X::getX() const (defined outside the class)
the function is prevented from modifying the object
– can’t change the object attributes
• When const appears in the parameter list,
int setNum (const int num)
the function is prevented from modifying the
parameter. The parameter is read-only.
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 48
© 2006 Pearson Education.
All Rights Reserved
The this Pointer and Constant
Member Functions
• this pointer:
- Implicit parameter passed to a member
function (by the compiler)
- points to the object calling the function
• const member function:
- does not modify its calling object
This pointer
• When a member function is called, an implicit
argument is automatically passed that is a
pointer to the invoking object (that is, object
on which the function is called). This type of
pointer is called this.
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 50
© 2006 Pearson Education.
All Rights Reserved
Using the this Pointer
• Can be used to access members that may
be hidden by parameters with same name:
class SomeClass
{
private:
int num;
public:
void setNum(int num)
{ this->num = num; }
};
Constant keyword
• In c++, constant keyword is used to make
program elements constant. Constant keyword
can be used with:
• Variable
• Pointer
• Function arguments and return types
• Class data member
• Class member function
• objects
C++
IT 3rd Sem
Object-Oriented Programming:
Types of Member Functions:
 Inline Functions
 Nested Functions
 Friend Functions
 Static Functions
 Virtual Functions
FRIEND FUNCTION
• The protected and private members cannot be
accessed from outside the same class at which
they are declared.
• Its possible to grant a non member function
access to the private members of a class, by
using a keyword friend.
Cont………
• A friend function has access to all private and
protected members of the class for which it is
friend.
Syntax of friend function
• Class rectangle
• {
• ……
• …..
• Public:
• ……
• ……
• Friend rectangle duplicate (rectangle)
• }
Characteristics of friend function
1. It is not member function of the class.
2. It is like normal external functions.
3. It is not in the scope of the class to which it
has been declared.
4. It can be declared either public or private
section of the class.
5. Usually it passes objects as arguments.
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 57
© 2006 Pearson Education.
All Rights Reserved
Friends of Classes
• Friend function: a function that is not a
member of a class, but has access to private
members of the class
• A friend function can be 1) a stand-alone
function or 2) a member function of another
class
• It is declared a friend of a class with the
friend keyword in the function prototype
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 58
© 2006 Pearson Education.
All Rights Reserved
Friend Class Declaration
3) An entire class can be declared a friend of a
class:
class aClass
{private:
int x;
friend class frClass;
};
class frClass
{public:
void fSet(aClass &c,int a){c.x = a;}
int fGet(aClass c){return c.x;}
};
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 59
© 2006 Pearson Education.
All Rights Reserved
Friend Class Declaration
• If frClass is a friend of aClass, then all
member functions of frClass have
unrestricted access to all members of
aClass, including the private members.
• In general, restrict the property of Friendship
to only those functions that must have access
to the private members of a class.
Nested class
• A class can be defined within other class, such
a class is called nested class.
• A member of its enclosing class.
• Its definition can occur within a public,
protected or private section of its enclosing
class.
Local classes
• A class that can be defined inside a function
body. Such a class is called local class.
• It is only visible in the local scope in which it is
defined.
Abstract class
• An abstract class is a class that is designed to
be specifically used as a base class.
• An abstract class contains at least one pure
virtual function.
• You declare a pure virtual function by using
a pure specifier (= 0) in the declaration of a
virtual member function in the class
declaration.
Cont………..
• You cannot create an object of an abstract
class type; however, you can use pointers and
references to abstract class types.
• A class that contains at least one pure virtual
function is considered an abstract class.
• Used to provide an interface to its sub classes.
Pure virtual functions
• Functions with no definition.
• They start with keyword virtual and ends with
= 0
• Syntax is:
• Virtual void f() = 0;
container classes
• A Container class is defined as a class that
gives you the power to store any type of data.
• There are two type of container classes in C++,
namely
• “Simple Container Classes” and
• “Associative Container Classes”.
• An Associative Container class associates a key
to each object to minimize the average access
time.
Cont………
• Simple Container Classes
* vector<>
* lists<>
* stack<>
* queue<>
* deque<>
Cont…………
• Associative Container Classes
* map<>
* set<>
* multimap<>
* multiset<>
Storage classes
• Used to specify the lifetime and scope of the
variables.
• How storage is allocated for variables and how
variable is treated by complier depending
upon these storage classes.
5 types
1. Local variable
2. Global variable
3. Register variable
4. Extern variable
5. Static variable
Namespace
• Container for identifiers.
• Puts the names of its member in a distinct
space so that they don’t conflict with the
names in other namespaces.
Syntax
• Its creation is similar to class creation
• Namespace Myspace
• {
• Declarations
• }
• Int main() {}
Will create namespace named Myspace, in
which we put member declarations.
Class member function
• A member function of a class is a function that
has its definition or its prototype within the
class definition like any other variable.
Class access modifier
• A class member can be defined as public,
private or protected. By default members
would be assumed as private.
Ad

Recommended

Anomalies in database
Anomalies in database
baabtra.com - No. 1 supplier of quality freshers
 
role of ICT in education
role of ICT in education
Vaibhav Dubey
 
Conditional and control statement
Conditional and control statement
narmadhakin
 
Artificial intelligence agents and environment
Artificial intelligence agents and environment
Minakshi Atre
 
Chapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating System
Shafaan Khaliq Bhatti
 
Desert ecosystem
Desert ecosystem
Vinitha Chandra Sekar
 
C vs c++
C vs c++
Gaurav Badhan
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
mshellman
 
Class and object in C++
Class and object in C++
rprajat007
 
Oop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Python-Inheritance.pptx
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
C# Delegates
C# Delegates
Raghuveer Guthikonda
 
classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Basic concept of OOP's
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
Moutaz Haddara
 
Java abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Java interfaces
Java interfaces
Raja Sekhar
 
C++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Basic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Object oriented programming
Object oriented programming
Amit Soni (CTFL)
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
friend function(c++)
friend function(c++)
Ritika Sharma
 
Pure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
JAVA AWT
JAVA AWT
shanmuga rajan
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
Shahzad Younas
 
class and objects
class and objects
Payel Guria
 

More Related Content

What's hot (20)

Class and object in C++
Class and object in C++
rprajat007
 
Oop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Python-Inheritance.pptx
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
C# Delegates
C# Delegates
Raghuveer Guthikonda
 
classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Basic concept of OOP's
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
Moutaz Haddara
 
Java abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Java interfaces
Java interfaces
Raja Sekhar
 
C++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Basic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Object oriented programming
Object oriented programming
Amit Soni (CTFL)
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
friend function(c++)
friend function(c++)
Ritika Sharma
 
Pure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
JAVA AWT
JAVA AWT
shanmuga rajan
 
Class and object in C++
Class and object in C++
rprajat007
 
Oop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
Moutaz Haddara
 
Java abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
C++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Basic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Object oriented programming
Object oriented programming
Amit Soni (CTFL)
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
friend function(c++)
friend function(c++)
Ritika Sharma
 
Pure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 

Viewers also liked (20)

Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
Shahzad Younas
 
class and objects
class and objects
Payel Guria
 
Bca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroduction
Rai University
 
Classes and objects
Classes and objects
Shailendra Veeru
 
Algorithms & flowcharts
Algorithms & flowcharts
baabtra.com - No. 1 supplier of quality freshers
 
User Defined Functions
User Defined Functions
Praveen M Jigajinni
 
System's Specification
System's Specification
Christine May Petajen-Brillantes
 
Pointers
Pointers
sanya6900
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
Sangharsh agarwal
 
Programming In C++
Programming In C++
shammi mehra
 
Classes And Objects
Classes And Objects
rahulsahay19
 
1. introduction to semantics
1. introduction to semantics
Asmaa Alzelibany
 
Flowchart
Flowchart
Gautam Roy
 
Friend function & friend class
Friend function & friend class
Abhishek Wadhwa
 
C++ Pointers
C++ Pointers
Chaand Sheikh
 
Unit 6 pointers
Unit 6 pointers
George Erfesoglou
 
functions of C++
functions of C++
tarandeep_kaur
 
Introduction to Algorithms
Introduction to Algorithms
Venkatesh Iyer
 
Data Structures and Algorithms
Data Structures and Algorithms
Pierre Vigneras
 
SEMANTICS
SEMANTICS
Hameel Khan
 
Ad

Similar to Classes and objects (20)

classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
cpp class unitdfdsfasadfsdASsASass 4.ppt
cpp class unitdfdsfasadfsdASsASass 4.ppt
nandemprasanna
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
Enam Khan
 
4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
 
class c++
class c++
vinay chauhan
 
Lecture 2 (1)
Lecture 2 (1)
zahid khan
 
Introduction to Class a deep analysisadfas
Introduction to Class a deep analysisadfas
Rayhan331
 
C++ Notes
C++ Notes
MOHAMED RIYAZUDEEN
 
classes data type for Btech students.ppt
classes data type for Btech students.ppt
soniasharmafdp
 
Class and object
Class and object
Prof. Dr. K. Adisesha
 
Class and objects
Class and objects
nafisa rahman
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
secondakay
 
ccc
ccc
Zainab Irshad
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Classes and objects
Classes and objects
Lovely Professional University
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
urvashipundir04
 
Classes, objects and methods
Classes, objects and methods
farhan amjad
 
22 scheme OOPs with C++ BCS306B_module1.pdf
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
cpp class unitdfdsfasadfsdASsASass 4.ppt
cpp class unitdfdsfasadfsdASsASass 4.ppt
nandemprasanna
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
Enam Khan
 
Introduction to Class a deep analysisadfas
Introduction to Class a deep analysisadfas
Rayhan331
 
classes data type for Btech students.ppt
classes data type for Btech students.ppt
soniasharmafdp
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
secondakay
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
urvashipundir04
 
Classes, objects and methods
Classes, objects and methods
farhan amjad
 
22 scheme OOPs with C++ BCS306B_module1.pdf
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Ad

Recently uploaded (20)

LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 

Classes and objects

  • 1. CLASSES AND OBJECTS ANIL KUMAR https://www.facebook.com/AniLK0221
  • 2. INTRODUCTION • C++ class mechanism allows users to define their own data types that can be used as conveniently as build in types. • Classes are often called user defined types. • Includes defining of class types and creation of objects of classes.
  • 3. Cont………… • The class definition introduces both : • the class data members that define the internal representation of class • The class member functions that define the set of operations that may be applied to objects of class type.
  • 4. Cont…………. • Information hiding is achieved by declaring the data members as private whereas the operations to be performed on class objects by the program are public.
  • 5. What is class? • Created using keyword class. • A class declaration defines a new user defined data type that links code and data. • The new data type is then used to declare objects of that class. • Class is a logical abstraction but an object has physical existence. • Objects is an instance of class.
  • 6. Cont………. • The class is the cornerstone of C++ – It makes possible encapsulation, data hiding and inheritance – A user defined type – Consists of both data and methods – Defines properties and behavior of that type
  • 7. Syntax of Class declaration • Class className • { • // body of a class • }
  • 8. Cont……………. • The class definition has two parts: • Class head : composed of keyword class followed by the class name • Class body: enclosed by a pair of curly braces.
  • 9. General form of class declaration • Class className • { • Access_specifier_1: • Members; • Access_specifier_2: • Members; • …… • } • objectName;
  • 10. Cont………… • The objectName is optional. • If present, it declares objects of the class.
  • 11. Example of student class • Class student • { • Int rollNo; (data members) • Char name [25]; • Char addr [35]; • Public: • Void getdata(); (member functions) • Void display(); • } • ;
  • 12. Cont……… • The class data members and member functions are declared within the body of the class along with their access levels. • The class body defined the class member list and their scope. • If two classes have members with the same name, the program will not show any error since the members refer to different objects.
  • 13. Access specifier • Public • Private • Protected
  • 14. Public • Public members may be accessed by member functions of same class and functions outside the scope of the class (anywhere inside the program)
  • 15. Private • Private members may only be accessed by member functions and friend function of the class. • A class that enforces information hiding declares its data members as private.
  • 16. Protected • The protected members may be accessed only by the member functions of its class or by member functions of its derived class.
  • 17. Cont……….. • The data hiding is achieved by declaring data members in the private section of the class. • Since the private members are not accessible from outside the class, they will be accessed by the publicly declared member functions of the same class.
  • 18. Defining a class class box { Public: Int a,b,c; Data Members void get() { cin>>a>>b>>c; } void put() Member Functions { cout<<a<<b<<c; } };
  • 19. C++ IT 3rd Sem Object-Oriented Programming: Class: class <class-name> { access-specifier: variable declarations function declarations access-specifier: variable declarations function declarations access-specifier: variable declarations function declarations };
  • 20. Example • Class rectangle • { • Int length; • Int width; • Public: • Void setvalues (int, int); • Int area(); • };
  • 21. Cont……….. • It declares a class rectangle. The class contains four members: • Two data members length and width of type integer in the private section (because private section is default permission) • Two member functions: • Setvalues (int, int) and area() in the public section.
  • 22. C++ IT 3rd Sem Object-Oriented Programming: Object: class <class-name> { access-specifier: variable declarations function declarations access-specifier: variable declarations function declarations } ob1; //object creation void main() { <class-name> ob2; //object creation
  • 23. Objects A class provides the blueprints for objects, so basically an object is created from a class. Object can be called as an instance of a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box: Box Box1; Box Box2;
  • 24. Cont……….. • The definition of class does not cause any storage to be allocated. Storage is only allocated when object of class type is defined. • The process of creating objects of the class is called class instantiation.
  • 25. Syntax of defining objects of a class • Class className objectName • Class : keyword • ClassName : user defined class name • User defined object name
  • 26. Object Operations All operations that can be applied to basic data types can be applied to the objects. E.g.: • Arithematic • Relational • Logical
  • 27. Example • Class rectangle • { • Int length; • Int width; • Public: • // member functions • }; • The definition • Rectangle rect;
  • 28. Cont……….. Will allocate the memory space sufficient to contain the two data members of the rectangle class. The name rect refers to that memory location. Each class object has its own copy of the class data members. • An object of a class type also has a lifetime.
  • 29. Data members • Declared in the same way as the variables are declared. • Generally, all data members of a class are made private to that class. • If data members are declared in the public section of the class, they will be accessed using member access operator, dot (.).
  • 30. Syntax of accessing data members of class • objectName . Data member • ObjectName : user defined object name • . : member access operator • Data member: data member of a class.
  • 31. Member functions • Functions that are declared within a class are called member functions. • Works on data members of class. • Member functions can access any element of the class of which they belong to. • The member functions of a class are declared inside the class body.
  • 32. Syntax of accessing member functions of a class • objectName . functionName (Actual Arguments) • objectName: user defined object name • . : member access operator • functionName: name of the member function • Actual arguments: arguments list to the function
  • 33. Cont………… • Member functions are declared within the scope of their class. It means member function is not visible outside the scope of its class.
  • 34. Member function can be defined in two ways • Inside the class • Outside the class
  • 35. Functions defined inside the class • Member function of a class can be defined inside the class declaration. • Its syntax is similar to a normal function definition except that it is enclosed within the body of a class.
  • 36. Example • The member function in the rectangle class can be defined as follows: • Class rectangle • { • Int length; • Int width; • Public: • Void setvalues(int x, int y) • { • Length = x; • Width = y; • } • Int area () • { • return(length * width); • } • } ;
  • 37. Cont……. • In this the class contains two member functions in the public section: Setvalues(int, int) Area() • They are defined inside the class itself.
  • 38. Functions defined outside the class • In this method, the prototype of the member function is declared within the body of the class and then defined outside the body of the class. • Functions defined outside the class have the same syntax as the normal function, there should be a mechanism of binding to the class to which they belong.
  • 39. Cont…………… • This requires a special declaration. • The name of the member function must be qualified by the name of its class by using the scope resolution operator. ( : : )
  • 40. General format of member function definition • Class className • { • …… • returnType memberFunction (arguments); • User defined class name • } • ;
  • 41. Member function definition outside the class • returnType className :: memberFunction (arguments) • { • // body of the function • }
  • 42. Object-Oriented Programming Using C++, Third Edition 42 Implementing Class Functions • When you construct a class, you create two parts: – Declaration section: contains the class name, variables (attributes), and function prototypes – Implementation section: contains the functions • Use both the class name and the scope resolution operator (::) when you implement a class function
  • 43. Object-Oriented Programming Using C++, Third Edition 43 Implementing Class Functions (continued)
  • 44. Object-Oriented Programming Using C++, Third Edition 44 Using Static Class Members • When a class field is static, only one memory location is allocated – All members of the class share a single storage location for a static data member of that same class • When you create a non-static variable within a function, a new variable is created every time you call that function • When you create a static variable, the variable maintains its memory address and previous value for the life of the program
  • 45. Static variable • Static variables are sometimes called class variables, class fields, or class-wide fields because they don’t belong to a specific object; they belong to the class. • Initialized and allocated storage only once at the beginning of the program execution. • No matters how many times they are called and used in the program. • Retains its value until the end of the program.
  • 46. Chapter 11 Starting Out with C++: Early Objects 5/e slide 46 © 2006 Pearson Education. All Rights Reserved Static Members • Static variables: - Shared by all objects of the class - Like a “global variable” among objects of the class • Static member functions: - Can be used to access static member variables - Can be called before any objects are created
  • 47. Chapter 11 Starting Out with C++: Early Objects 5/e slide 47 © 2006 Pearson Education. All Rights Reserved Constant Member Functions • Declared with keyword const • When const follows the parameter list, int getX() const; (in the class definition) int X::getX() const (defined outside the class) the function is prevented from modifying the object – can’t change the object attributes • When const appears in the parameter list, int setNum (const int num) the function is prevented from modifying the parameter. The parameter is read-only.
  • 48. Chapter 11 Starting Out with C++: Early Objects 5/e slide 48 © 2006 Pearson Education. All Rights Reserved The this Pointer and Constant Member Functions • this pointer: - Implicit parameter passed to a member function (by the compiler) - points to the object calling the function • const member function: - does not modify its calling object
  • 49. This pointer • When a member function is called, an implicit argument is automatically passed that is a pointer to the invoking object (that is, object on which the function is called). This type of pointer is called this.
  • 50. Chapter 11 Starting Out with C++: Early Objects 5/e slide 50 © 2006 Pearson Education. All Rights Reserved Using the this Pointer • Can be used to access members that may be hidden by parameters with same name: class SomeClass { private: int num; public: void setNum(int num) { this->num = num; } };
  • 51. Constant keyword • In c++, constant keyword is used to make program elements constant. Constant keyword can be used with: • Variable • Pointer • Function arguments and return types • Class data member • Class member function • objects
  • 52. C++ IT 3rd Sem Object-Oriented Programming: Types of Member Functions:  Inline Functions  Nested Functions  Friend Functions  Static Functions  Virtual Functions
  • 53. FRIEND FUNCTION • The protected and private members cannot be accessed from outside the same class at which they are declared. • Its possible to grant a non member function access to the private members of a class, by using a keyword friend.
  • 54. Cont……… • A friend function has access to all private and protected members of the class for which it is friend.
  • 55. Syntax of friend function • Class rectangle • { • …… • ….. • Public: • …… • …… • Friend rectangle duplicate (rectangle) • }
  • 56. Characteristics of friend function 1. It is not member function of the class. 2. It is like normal external functions. 3. It is not in the scope of the class to which it has been declared. 4. It can be declared either public or private section of the class. 5. Usually it passes objects as arguments.
  • 57. Chapter 11 Starting Out with C++: Early Objects 5/e slide 57 © 2006 Pearson Education. All Rights Reserved Friends of Classes • Friend function: a function that is not a member of a class, but has access to private members of the class • A friend function can be 1) a stand-alone function or 2) a member function of another class • It is declared a friend of a class with the friend keyword in the function prototype
  • 58. Chapter 11 Starting Out with C++: Early Objects 5/e slide 58 © 2006 Pearson Education. All Rights Reserved Friend Class Declaration 3) An entire class can be declared a friend of a class: class aClass {private: int x; friend class frClass; }; class frClass {public: void fSet(aClass &c,int a){c.x = a;} int fGet(aClass c){return c.x;} };
  • 59. Chapter 11 Starting Out with C++: Early Objects 5/e slide 59 © 2006 Pearson Education. All Rights Reserved Friend Class Declaration • If frClass is a friend of aClass, then all member functions of frClass have unrestricted access to all members of aClass, including the private members. • In general, restrict the property of Friendship to only those functions that must have access to the private members of a class.
  • 60. Nested class • A class can be defined within other class, such a class is called nested class. • A member of its enclosing class. • Its definition can occur within a public, protected or private section of its enclosing class.
  • 61. Local classes • A class that can be defined inside a function body. Such a class is called local class. • It is only visible in the local scope in which it is defined.
  • 62. Abstract class • An abstract class is a class that is designed to be specifically used as a base class. • An abstract class contains at least one pure virtual function. • You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
  • 63. Cont……….. • You cannot create an object of an abstract class type; however, you can use pointers and references to abstract class types. • A class that contains at least one pure virtual function is considered an abstract class. • Used to provide an interface to its sub classes.
  • 64. Pure virtual functions • Functions with no definition. • They start with keyword virtual and ends with = 0 • Syntax is: • Virtual void f() = 0;
  • 65. container classes • A Container class is defined as a class that gives you the power to store any type of data. • There are two type of container classes in C++, namely • “Simple Container Classes” and • “Associative Container Classes”. • An Associative Container class associates a key to each object to minimize the average access time.
  • 66. Cont……… • Simple Container Classes * vector<> * lists<> * stack<> * queue<> * deque<>
  • 67. Cont………… • Associative Container Classes * map<> * set<> * multimap<> * multiset<>
  • 68. Storage classes • Used to specify the lifetime and scope of the variables. • How storage is allocated for variables and how variable is treated by complier depending upon these storage classes.
  • 69. 5 types 1. Local variable 2. Global variable 3. Register variable 4. Extern variable 5. Static variable
  • 70. Namespace • Container for identifiers. • Puts the names of its member in a distinct space so that they don’t conflict with the names in other namespaces.
  • 71. Syntax • Its creation is similar to class creation • Namespace Myspace • { • Declarations • } • Int main() {} Will create namespace named Myspace, in which we put member declarations.
  • 72. Class member function • A member function of a class is a function that has its definition or its prototype within the class definition like any other variable.
  • 73. Class access modifier • A class member can be defined as public, private or protected. By default members would be assumed as private.