C++ Session 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

CLASSES 1

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, and optional access
specifiers.
An object is an instantiation of a class. In terms of
variables, a class would be the type, and an
object would be the variable.
Definition
• Classes are defined using the keyword class just like
keyword struct for structure, with the following syntax:

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
class Rectangle { object (i.e., a variable) of this
//default access class, called rect.
//data members • This class contains four
members: two data members
int width, height;
of type int (members width
//public access and height) with private
public: access (because private is the
default access level) and
//function prototypes
• Two member functions with
void set_values (int,int); public access: the functions
int area (void); set_values and area, of which
} rect;//object for now we have only
included their declaration,
but not their definition.
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> {
using namespace std; return width*height;
}
}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;
} }
Semantic 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();
} return 0; }
Semantic Error
More object outside the class
// example: two objects
#include <iostream> int main () {
using namespace std; Rectangle Mango, Banana;
class Rectangle { Mango.set_values (3,4);
int width, height;
public:
Banana.set_values (5,6);
void set_values (int a,int b)
{
cout << "Mango area: " <<
Mango.area() << endl;
width = a;
height = b;
} cout << "Banana area: " <<
int area () Banana.area() << endl;
{return width*height;} return 0;
}; }
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, two //scope operator
//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;
}; return 0;
}
What would happen if we called the function
area before calling set_value?

We would have undetermined results because


width and height wouldn’t be assigned values.

We would avoid this by including what is called a


constructor.
A constructor function would be declared just as
the member function but with the same name
as the class name and without any return or
void type
The results of this example are identical to those
of the previous example.
But now, class Rectangle has no member
function set_values, and has instead a
constructor that performs a similar action: it
initializes the values of width and height with
the arguments passed to it
Default constructor takes no parameters
Overloading a constructor
#include <iostream> //overloaded Constructor
using namespace std; Rectangle::Rectangle (int a, int b)
class Rectangle { {
int width, height; width = a;
public: height = b;
Rectangle ();//default header
//constructor }
//overloaded header constructor int main () {
Rectangle (int,int); Rectangle rect (3,4);//overload
int area (void) {return Rectangle rectb;//default
(width*height);}
cout << "rect area: " <<
}; rect.area() << endl;
//Constructor
cout << "rectb area: " <<
Rectangle::Rectangle () {
rectb.area() << endl;
width = 5;
height = 5;
return 0;
} }
rect area: 12
rectb area: 25
#include <iostream>
using namespace std;
class Demo{
public:
int num; num initialized to: 100
char ch; ch initialized to: A
Demo() {
num = 100; ch = 'A'; }
};
int main(){
Demo obj;
cout<<"num initialized to: "<<obj.num<<endl;
cout<<"ch initialized to: "<<obj.ch;
return 0; }
#include <iostream>
using namespace std;
class site{
public:
Welcome to CS103
//Default constructor
Welcome to CS103
site() {
cout<<"Welcome to CS103"<<endl; }
};
int main(){
site obj1;
site obj2;
return 0;
}
#include <iostream>
using namespace std;
class simple{
sum of 50 and 60 is 110
public:
simple(int num1, int num2)
{
cout<<"sum of "<<num1<<" and "
<<num2<<" is "<<(num1+num2)<<endl; }
};
int main(){
simple obj2 = simple(50, 60);
return 0; }
class Cylinder {
#include <iostream> Circle base;
double height;
using namespace std; public:
Cylinder(double r, double h) : base
(r), height(h) {}
class Circle { double volume() {return
double radius; base.area() * height;}
};
public:
Circle(double r) : radius(r) { } int main () {
double area() {return Cylinder foo (10,20);
radius*radius*3.14159265;}
cout << "foo's volume: " <<
}; foo.volume() << '\n';
return 0;
}
foo’s volume: 6283.19

You might also like