You are on page 1of 20

Concept of classes and

objects

Class
A class is a user define data type which
holds both data and function.
The data included in the class i.e the
internal data is called the internal data
or data member and the functions
included is called the member function.
These member functions can manipulate
the internal data of the class

Object
Is an instant of a class.
In terms of variables, class would be the
type and an object would be a variable.

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 semicolon).
class class_name
{
.
.
.
};

Any valid
identifier
Class body (data
member + methods)
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 (any
user define name). It can also be called
as tag name of the class that act as the
type specifier for 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.
E.g. int x;

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:

};

private members or
methods

Public members or
methods

Private:
only members of that class have
accessibility
can be accessed only through member
functions of that class i.e by the functions
declared inside the class only.

Private members and methods are for


internal use only.

Public:
Accessible from both inside and outside the class
also i.e by the functions declared in the main()
program also.
Protected:
Stage between private and public access.
They can be accessed by the member function or
friend functions of the class. They are similar to
private members in the sense that they cannot be
accessed by the non- member functions of the class.

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 outsid


the class, and they can access
member (radius)

Methods definition
The member function of the class can be defined in
two different ways:
1)Inside the class definition:- The member
functions are simple defined inside the class only i.e
the body of the function resides inside the range of
class only.
2) Outside the class definition: by using scope
resolution operator, which specifies that the scope
of the function is restricted to the class class_name.
Syntax:- class_name:: function_name

Inside the class definition


Eg: class abc
{

Cout<<rollno=;

Private:

Cin>>rollno;

Int rollno;

Char name[20];

Void display()

Public:

Void getdata()

Cout<<name=<<name;

Cout<<rollno=<<rollno;

Cout<<name=;

Cin>>name;

};

Outside the class definition


Eg: class abc

Cin>>name;

Cout<<rollno=;

Private:

Cin>>rollno;

Int rollno;

Char name[20];

Void abc :: display()

Public:

Void getdata();

Cout<<name and rollno=;

Void display();

Cout<<name<<rollno;

};

Void abc :: getdata()


{
Cout<<name=;

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- class Circle c;

Class book

Void book ::display()

Private:

Cout<<book name=<<n;

Int p; char n[40];

Cout<<book price=<<p;

Public:
Void getdata()
{
Cout<<enter book price;
Cin>>p;

}
Void main()
{
Class book obj;

Cout<<enter book name;

Obj.getdata();

Cin>>n; }

Obj.display();

Void display(); };

Object definition using array


Instead of declaring each object
individually an array can be implemented
in declaration of objects.
Class data
{
--------- ;
--------- ;
};
Class data obj[n];

EXAMPLE
Cin>>p>>n;
}

Void main()

Void display()

Class book

Cout<<name and price=<<n<<p;

} };
Class book obj[50];

Private:

Int;

Int p;char n [30];

For(i=0;i<50;i++)

Public:

Void getdata()
{
Cout<<enter name and price;

Obj[i].getdata();
}
Cout<<enterd records are;
For(i=0;i<50;i++)
{
Obj[i].display; } }

You might also like