C++ Session 10
C++ Session 10
By Charles Saah
[email protected]
Classes
Classes are an expanded concept of data
structures: like data structures, they can contain
data members, but they can also contain
functions as members.
class class_name {
access_specifier_1:
member1;
member2;
access_specifier_2:
member3;
...
} object_names;
Classes Cont.
• Where class_name is a valid identifier for the
class, object_names is an optional list of
names for objects of this class. The body of
the declaration can contain members, which
can either be data or function declarations,
and optionally access specifiers
Classes Cont.
• Classes have the same format as plain data
structures, except that they can also include
functions and have these new things called
access specifiers.
• An access specifier is one of the following
three keywords: private, public or protected.
• These specifiers modify the access rights for
the members that follow them:
Access Specifiers
• Private members of a class are accessible only
from within, and other members of the same
class (You cannot access it outside of the class.).
• Protected members are accessible from other
members of the same class (or from their
"friends"), but also from members of their
derived classes.
• Public members are accessible from anywhere,
where the object is visible.
Classes Cont.
• By default, all members of class Rectangle {
a class declared with the //default access-private
class keyword have int width, height;
private access for all its
public:
members. Therefore, any
member that is declared void set_values (int,int);
before any other access int area (void);
specifier has private } rect;
access automatically
• For example
Explanation • Declares a class (i.e., a type)
called Rectangle and an object
class Rectangle { (i.e., a variable) of this class,
//default access called rect.
//data members • This class contains four
int width, height; members: two data members
//public access of type int (members width and
height) with private access
public: (because private is the default
//function prototypes access level) and
void set_values (int,int); • Two member functions with
int area (void); public access: the functions
set_values and area, of which
} rect;//object
for now we have only included
their declaration, but not their
Classes Cont.
Notice the difference between the class name
and the object name: Rectangle was the class
name (i.e., the type), whereas rect was an
object of type Rectangle. It is the same
relationship int and “a” have in the following
declaration:
int a;
Where int is the type name (ie class) and ‘a’ is
the variable name (the object)
After the declarations of Rectangle and rect, any of
the public members of object rect can be accessed
as if they were normal functions or normal
variables, by simply inserting a dot (.) between
object name and member name.
This follows the same syntax as accessing the
members of plain data structures
For example:
rect.set_values (3,4);
myarea = rect.area();
• The only members of rect that cannot be
accessed from outside the class are width and
height, since they have private access and
they can only be referred to from within other
members of that same class.
• Encapsulation will allow such members to be
accessed
Example int area ()
#include <iostream> {
return width*height;
using namespace std;
}
}rect; //object variable
class Rectangle int main()
{ //default identifier {Rectangle Chelsea;
int width, height; rect.set_Values(3,4);
Chelsea.set_Values(9,6);
public://identifier
void set_Values(int a, int b) cout<<"The area of rectangle is
{ "<<rect.area()<<endl;
width= a; cout<<"The area of rectangle2 is
"<<Chelsea.area();
height = b; return 5;
Simantice error and No object defined
#include <iostream> int area ()//function
using namespace std; {
class Rectangle return width*height; }
{//default identifier };// no object defined
int width, height;
int main()
public:
{
//function
void set_Values(int a, int b)
//variable of type Rectangle
{ Rectangle rect;
//a = width simantic error rect.set_Values(3,4);
a=width; cout<<"The area of
b=height; rectangle is "<<rect.area();
Semantic Error
More object outside the class
// example: two objects
int main () {
#include <iostream>
Rectangle Mango, Banana;
using namespace std;
Mango.set_values (3,4);
class Rectangle {
Banana.set_values (5,6);
int width, height;
cout << "Mango area: " <<
public: Mango.area() << endl;
void set_values (int a,int b) cout << "Banana area: " <<
{ Banana.area() << endl;
width = a; return 0;
height = b; }
}
int area ()
{return width*height;} Mango area: 12
Banana area: 30
Conversely,if set_values declared with its
prototype within the class, but its definition is
outside it. In this outside definition, the
operator of scope operator (::) is used to specify
that the function being defined is a member of
the class Rectangle
The scope operator (::) specifies the class to
which the member being declared belongs,
granting exactly the same scope properties as if
this function definition was directly included
within the class definition
More objects outside the class
// example: one class, //scope operator
two //objects void Rectangle::set_values (int x, int
y) {
#include <iostream> width = x;
using namespace std; height = y;
Mango area: 12
}
Banana area: 30
class Rectangle {
int main () {
int width, height; Rectangle Mango, Banana;
public: Mango.set_values (3,4);
Banana.set_values (5,6);
void set_values (int,int);
cout << "Manago area: " <<
int area () {return Mango.area() << endl;
width*height;} cout << "Banana area: " <<
Banana.area() << endl;
};
What would happen if we called the function
area before calling set_value?