0% found this document useful (0 votes)
62 views

4-Classes and Objects

The document discusses classes and objects in C++. Some key points: - A class defines the structure and behavior of an object. It groups together data (attributes) and functions (methods) that work on that data. - An object is an instance of a class in memory, with its own set of data values. Objects are created from classes using new. - Classes support encapsulation by declaring data as private and functions as public, hiding the data from direct access outside the class. - Member functions defined inside the class have access to private data. They are called on objects to set/get values. - The main() function creates a Rectangle object and calls its set_values() and area

Uploaded by

KISHORE K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

4-Classes and Objects

The document discusses classes and objects in C++. Some key points: - A class defines the structure and behavior of an object. It groups together data (attributes) and functions (methods) that work on that data. - An object is an instance of a class in memory, with its own set of data values. Objects are created from classes using new. - Classes support encapsulation by declaring data as private and functions as public, hiding the data from direct access outside the class. - Member functions defined inside the class have access to private data. They are called on objects to set/get values. - The main() function creates a Rectangle object and calls its set_values() and area

Uploaded by

KISHORE K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Classes and Objects

Imagine a tollbooth at a bridge. Cars passing by the booth are


expected to pay a 50 cent toll. Mostly they do, but sometimes a
car goes by without paying. The tollbooth keeps track of the
number of cars that have gone by, and the total amount of
money collected.
In the railway reservation system, users
can get both the train details and the train
reservation details. The details to be
provided for train are train no., train name,
boarding point, destination point, no. of
seats in first class and fare per ticket, no. of
seats in second class and fare per ticket and
date of travel.
When we take a mobile as an object, its basic functionality
for which it was invented were Calling & Receiving a call &
Messaging. But now a days thousands of new features &
models were added & the count is still increasing.
In above diagram, each brand (Samsung, Nokia, IPhone)
have their own list of features along with basic functionality
of dialing, receiving a call & messaging.
When we talk about OOP, as the word indicate it will talk
about an object (a real world object)
object
Any real world entity which can have some
characteristics or which can perform some work is
called as Object. This object is also called as an
instance i.e. - a copy of entity in programming
language.
If we consider the above example, a mobile
manufacturing company, at a time manufactures lacs
of pieces of each model which are actually an instance.

These objects are differentiated from each other via


some identity or its characteristics. This characteristics
is given some unique name.

⚫Mobile mbl1 = new Mobile ();


class
A Class is a plan which describes the object. We call it
as a blue print of how the object should be represented.

Mainly a class would consist of a name, attributes &


operations.

Considering the above example,


A Mobile can be a class which has some
attributes like Profile Type, IMEI Number, Processor,
and some more.) & operations like Dial, Receive &
SendMessage.
Classes
⚫A class is a way to bind the data & its
associated functions together.
⚫It allows the data & function to be hidden, if
necessary from external use.
⚫Generally a class specification has two parts :
1.Class Declaration – it describes the type &
scope of its members.
2.Class function definition – it describes how the
class function are implemented.
Declaring a class
The general form of a class declaration is-
Class class_name
{
private:
variable declaration; // data members or class
members
function declaration;
public :
variable declaration; // Member functions
function declaration;
};
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)
};
Classes in C++
⚫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


};
Classes in C++
⚫Member access specifiers
◦ public:
●can be accessed outside the class directly.
●The public stuff is the interface.
◦ private:
●Accessible only to member functions of class
●Private members and methods are for internal
use only.
Access specifiers
Access specifiers
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
class Circle access and retrieve its value
directly. The
{
class methods are responsible for
private: that only.
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();
};
Defining the Class
test

somedata

50 setdata)(d)
showdata()

50
Creating an object of a Class
⚫Declaring a variable of a class type creates an
object. You can have many variables of the same type
(class).
◦ Instantiation

⚫Once an object of a certain class is instantiated, a


new memory location is created for it to store its data
members and code
⚫You can instantiate many objects from a class type.
◦ Ex) Circle c; test t;
Define Object from class

classname objectname Example: test s1, s2;

somedata

s1 seddata)(d)
showdata()
Objects are
test sometimes called
instance variables.

somedata

s2 seddata)(d)
showdata()
Complete Example
Call Member Function
int somedata;
void setdata(int d)
{
s1.setdata(1066);
somedata = d;
}
s2.setdata(1776);
smallobj s1 void showdata()
{
cout << “Data is “ << somedata; setdata(1066);
}; setdata(1776);
smallobj

int somedata;
void setdata(int d)
smallobj s2 {
somedata = d;
} s1.showdata();
void showdata() s2.showdata();
{
cout << “Data is “ << somedata;
};
Creating and Using Rectangle
Class
// classes example void CRectangle::set_values (int a, int b)
#include <iostream> {
width = a;
height = b;
class CRectangle { }
int width, height;
public: int main ()
void set_values (int,int); {
int area () CRectangle rect;
{ rect.set_values (3,4);
return (width*height); cout << "area: " << rect.area();
} return 0;
}; }

26
Creating and Using Rectangle
Declares a class called
Class
CRectangle
Everything inside this opening
brace and the corresponding
closing
void brace is part of to the (int a, int b)
CRectangle::set_values
// classes example
{ class
#include <iostream>
using namespace std; width = a;
height = b;
class CRectangle { }
int width, height;
public: int main ()
void set_values (int,int); {
int area () CRectangle rect;
{ rect.set_values (3,4);
return (x*y); cout << "area: " << rect.area();
} return 0;
}; }
Closing brace
for the class

29
Creating and Using Rectangle
Class
// classes example void CRectangle::set_values (int a, int b)
#include <iostream> {
using namespace std; width = a;
height = b;
class CRectangle { }
int width, height;
public: int main ()
void set_values (int,int); {
int area () CRectangle rect;
{ rect.set_values (3,4);
Function
return (x*y); definition for set_values
cout <<is"area:
done " << rect.area();
} outside the CRectangle class return 0;
definition itself.
}; We must use the operator} of scope (::) to
specify that we are defining a function that is
a member of the class CRectangle

30
Creating and UsingCreRectangle
ate a
type n ob
Classram C Re
ctan
j ec t (
gle.
varia
ble)
of
p r og
in
ma
r
O// classes example
u void CRectangle::set_values (int a, int b)
#include <iostream> {
using namespace std; width = a;
height = b;
class CRectangle { }
int width, height;
public: int main ()
void set_values (int,int); {
int area () CRectangle rect;
{ rect.set_values (3,4);
return (x*y); cout << "area: " << rect.area();
} return 0;
}; }
To access rect’s public members
we use the object's name (rect)
followed by a dot (.) and then the
name of the member
31

You might also like