You are on page 1of 18

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 Any valid identifier


{
….
….
Class body (data member +
….
methods)
};
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: private members or methods



public:
… Public 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)
No need for others classes to access
class Circle and retrieve its value directly. The
class methods are responsible for
{
that only.
private:
double radius;
public:
void setRadius(double r); They are accessible from outside
double getDiameter(); the class, and they can access the
double getArea(); member (radius)
double getCircumference();
};
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;

You might also like