SlideShare a Scribd company logo
CLASSES AND OBJECTS
Class
• A class is an organization of data and functions
which operate on them.
• Data structures are called data members and the
functions are called member functions.
• The combination of data members and member
functions constitute a data object or simply an
object.
Object
• Instantiation of a class.
• In terms of variables, class would be the type and
an object would be a variable.
General Structure of a class
• Class name or name of class
• Data Members
• Member functions
• Access Specifiers
• Declaring objects
Classes in C++
• A class definition begins with the keyword class.
• The body of the class is contained within a set of
braces, { } ; (notice the semi-colon).
class class_name
{
….
….
….
};
Class body (data member +
methods)
Any valid
identifier
class classname
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
protected:
variable declarations;
function declarations;
} obj1, obj2,…..objN;
Class name
• Name given to a particular class.
• Serves as a name specifier for the class using which
we can create objects.
• The class is specified by keyword “class”
Data Members
• Data type properties that describe the
characteristics of a class.
• We can declare any number of data members of
any type in a class.
• We can say that variables in C and data members in
C++.
• E.g. int rn;
Member functions
• Various operations that can be performed to data
members of that class.
• We can declare any number of member functions
of any type in a class.
• E.g. void read();
Access Specifiers
Used to specify access rights for the data members
and member functions of the class.
Depending upon the access level of a class member,
access to it is allowed or denied.
Within the body, the keywords private: and public:
specify the access level of the members of the class.
the default is private.
Usually, the data members of a class are declared in
the private: section of the class and the member
functions are in public: section.
Classes in C++
class class_name
{
private:
…
…
…
public:
…
…
…
};
Public members or methods
private members or
methods
Private:
only members of that class have accessibility
 can be accessed only through member functions of
that class.
Private members and methods are for internal
use only.
Public:
• Accessible from outside the class
• can be accessed through member function of any
class in the same program.
Protected:
• Stage between private and public access.
• They cannot be accessed from outside the class, but
can be accessed from the derived
class.(inheritance)
Class Example
• This class example shows how we can encapsulate
(gather) a circle information into one package (unit
or class)
class Circle
{
private:
double radius;
public:
void setRadius(double r);
double getDiameter();
double getArea();
double getCircumference();
};
No need for others classes to
access and retrieve its value
directly. The
class methods are responsible for
that only.
They are accessible from outside
the class, and they can access the
member (radius)
C++ supports three access specifiers:
public
private
protected
The public access specifier allows a class to subject its
member variables and member functions to other functions
and objects
The private access specifier allows a class to hide its
member variables and member functions from other class
objects and functions
The protected access specifier allows a class to hide its
member variables and member functions from other class
objects and functions just like private access specifier - is
used while implementing inheritance
Declaring objects:
A class declaration only uses to build the structure
of an object.
Declaration of an object is same as declaration of
class.
Defining objects of class data type is known as class
instantiation(instances of class)
When we create objects during that moment ,
memory is allocated to them.
• Ex- Circle c;
class Customer
{
void accept()
{
cout << “Accepting Customer Details” << endl;
}
void display()
{
cout << “Displaying Customer Details” << endl;
}
};
Void main()
{
Customer C1;
C1.accept();
C1.display();
getch();
}
#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
char name[10];
public:
void get();
void display();
};
void student::get()
{
cout<<"Enter Roll Number=";
cin>>rollno;
cout<<"Enter Name=";
cin>>name;
}
void student::display()
{
cout<<"Roll Number="<<rollno<<endl;
cout<<"Name="<<name<<endl;
}
int main()
{
student s1;
clrscr();
s1.get();
s1.display();
getch();
return 0;
}

More Related Content

PDF
Class and object
PPTX
Inline Functions and Default arguments
PPTX
Template C++ OOP
PPT
friend function(c++)
PPTX
class and object in c++.pptx
PPTX
Access specifier
PPT
OOP in C++
PPTX
classes and objects in C++
Class and object
Inline Functions and Default arguments
Template C++ OOP
friend function(c++)
class and object in c++.pptx
Access specifier
OOP in C++
classes and objects in C++

What's hot (20)

PPTX
Inheritance in c++
PPTX
Data members and member functions
PPT
Inheritance
PPTX
virtual function
PPTX
This pointer
PPTX
concept of oops
PPTX
inheritance c++
PPTX
PPTX
Operator overloading
PPTX
Constructor and destructor
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PDF
file handling c++
PPTX
Pure virtual function and abstract class
PPTX
Constructor and Types of Constructors
PPTX
07. Virtual Functions
PPTX
Virtual function in C++ Pure Virtual Function
PDF
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
PPTX
Inter Thread Communicationn.pptx
PPTX
Abstract class in c++
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Inheritance in c++
Data members and member functions
Inheritance
virtual function
This pointer
concept of oops
inheritance c++
Operator overloading
Constructor and destructor
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
file handling c++
Pure virtual function and abstract class
Constructor and Types of Constructors
07. Virtual Functions
Virtual function in C++ Pure Virtual Function
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
Inter Thread Communicationn.pptx
Abstract class in c++
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Ad

Similar to Classes and objects (20)

PPTX
object oriented programming-classes and objects.pptx
PPT
classes data type for Btech students.ppt
PPTX
Classes and objects
PPTX
PPT
Class and object in C++
PPT
4 Classes & Objects
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
PPTX
class c++
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PPT
c++ introduction
PPT
c++ lecture 1
PPT
c++ lecture 1
PDF
Chapter18 class-and-objects
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
PPTX
C++ And Object in lecture3
PPT
Classes in C++ computer language presentation.ppt
PPTX
C# classes objects
PPTX
Class and object
PPTX
Java Inheritance - sub class constructors - Method overriding
PPTX
Class and objects
object oriented programming-classes and objects.pptx
classes data type for Btech students.ppt
Classes and objects
Class and object in C++
4 Classes & Objects
cpp class unitdfdsfasadfsdASsASass 4.ppt
class c++
classandobjectunit2-150824133722-lva1-app6891.ppt
c++ introduction
c++ lecture 1
c++ lecture 1
Chapter18 class-and-objects
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
C++ And Object in lecture3
Classes in C++ computer language presentation.ppt
C# classes objects
Class and object
Java Inheritance - sub class constructors - Method overriding
Class and objects
Ad

More from Carelon Global Solutions (20)

PPT
Constructors and destructors in C++ part 2
PPT
Constructor and destructor in C++
PPT
PPT
Virtual Function
DOC
Major project synopsis format
DOCX
Docslide.net soyabean milk-project-class-12
DOCX
Physics first page
DOCX
Uses of transformer
DOCX
Theory and working
DOCX
Main page vishnu
DOCX
Main page v physics
DOCX
Main page saurabh
DOCX
DOCX
Efficiency and energy losses
DOCX
DOCX
DOCX
Acknowledgement
DOCX
Principle construction
Constructors and destructors in C++ part 2
Constructor and destructor in C++
Virtual Function
Major project synopsis format
Docslide.net soyabean milk-project-class-12
Physics first page
Uses of transformer
Theory and working
Main page vishnu
Main page v physics
Main page saurabh
Efficiency and energy losses
Acknowledgement
Principle construction

Recently uploaded (20)

PPTX
Geodesy 1.pptx...............................................
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Well-logging-methods_new................
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Lecture Notes Electrical Wiring System Components
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Project quality management in manufacturing
PPTX
web development for engineering and engineering
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Geodesy 1.pptx...............................................
UNIT 4 Total Quality Management .pptx
Well-logging-methods_new................
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Lecture Notes Electrical Wiring System Components
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Construction Project Organization Group 2.pptx
Foundation to blockchain - A guide to Blockchain Tech
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Project quality management in manufacturing
web development for engineering and engineering
UNIT-1 - COAL BASED THERMAL POWER PLANTS

Classes and objects

  • 2. Class • A class is an organization of data and functions which operate on them. • Data structures are called data members and the functions are called member functions. • The combination of data members and member functions constitute a data object or simply an object.
  • 3. Object • Instantiation of a class. • In terms of variables, class would be the type and an object would be a variable.
  • 4. General Structure of a class • Class name or name of class • Data Members • Member functions • Access Specifiers • Declaring objects
  • 5. Classes in C++ • A class definition begins with the keyword class. • The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. …. …. }; Class body (data member + methods) Any valid identifier
  • 6. class classname { private: variable declarations; function declarations; public: variable declarations; function declarations; protected: variable declarations; function declarations; } obj1, obj2,…..objN;
  • 7. Class name • Name given to a particular class. • Serves as a name specifier for the class using which we can create objects. • The class is specified by keyword “class”
  • 8. Data Members • Data type properties that describe the characteristics of a class. • We can declare any number of data members of any type in a class. • We can say that variables in C and data members in C++. • E.g. int rn;
  • 9. Member functions • Various operations that can be performed to data members of that class. • We can declare any number of member functions of any type in a class. • E.g. void read();
  • 10. Access Specifiers Used to specify access rights for the data members and member functions of the class. Depending upon the access level of a class member, access to it is allowed or denied. Within the body, the keywords private: and public: specify the access level of the members of the class. the default is private. Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.
  • 11. Classes in C++ class class_name { private: … … … public: … … … }; Public members or methods private members or methods
  • 12. Private: only members of that class have accessibility  can be accessed only through member functions of that class. Private members and methods are for internal use only.
  • 13. Public: • Accessible from outside the class • can be accessed through member function of any class in the same program.
  • 14. Protected: • Stage between private and public access. • They cannot be accessed from outside the class, but can be accessed from the derived class.(inheritance)
  • 15. Class Example • This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius)
  • 16. C++ supports three access specifiers: public private protected The public access specifier allows a class to subject its member variables and member functions to other functions and objects The private access specifier allows a class to hide its member variables and member functions from other class objects and functions The protected access specifier allows a class to hide its member variables and member functions from other class objects and functions just like private access specifier - is used while implementing inheritance
  • 17. Declaring objects: A class declaration only uses to build the structure of an object. Declaration of an object is same as declaration of class. Defining objects of class data type is known as class instantiation(instances of class) When we create objects during that moment , memory is allocated to them. • Ex- Circle c;
  • 18. class Customer { void accept() { cout << “Accepting Customer Details” << endl; } void display() { cout << “Displaying Customer Details” << endl; } }; Void main() { Customer C1; C1.accept(); C1.display(); getch(); }
  • 19. #include<iostream.h> #include<conio.h> class student { private: int rollno; char name[10]; public: void get(); void display(); }; void student::get() { cout<<"Enter Roll Number="; cin>>rollno; cout<<"Enter Name="; cin>>name; } void student::display() { cout<<"Roll Number="<<rollno<<endl; cout<<"Name="<<name<<endl; } int main() { student s1; clrscr(); s1.get(); s1.display(); getch(); return 0; }