4-Classes and Objects
4-Classes and Objects
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
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