Object Oriented Programming - Objects and Classes
Object Oriented Programming - Objects and Classes
A Simple Class
// smallobj.cpp demonstrates a small, simple object
#include <iostream>
using namespace std;
class smallobj
// define a class (a concept)
{
private:
int somedata;
// class data (attributes)
public:
void setdata(int d){ // member function (method)to
somedata = d; // set data
}
void showdata(){ // member function to display data
cout << "Data is " << somedata << endl;
}
};
2
A Simple Class
int main()
{
smallobj s1, s2; // define two objects of class smallobj
s1.setdata(1066); // call member function to set data
s2.setdata(1776);
s1.showdata();
// call member function to display data
s2.showdata();
return 0;
}
(1/2)
(1/2)
Circles as Objects
// circles.cpp circles as graphics objects
#include "msoftcon.h"
// for graphics functions
class circle{
// graphics circle
private:
int xCo, yCo;
// coordinates of center
int radius;
// color
color fillcolor;
fstyle fillstyle;
// fill pattern
public:
// sets circle attributes
void set(int x, int y, int r, color fc, fstyle fs){
xCo = x;
yCo = y;
radius = r;
fillcolor = fc;
fillstyle = fs;
}
void draw(){
// draws the circle
set_color(fillcolor);
// set color
set_fill_style(fillstyle); // set fill
12
Circles as Objects
draw_circle(xCo, yCo, radius);// draw solid circle
}
};
int main(){
init_graphics();
// initialize graphics system
circle c1, c2, c3;
// create circles
//set circle attributes
c1.set(15, 7, 5, cBLUE, X_FILL);
c2.set(41, 12, 7, cRED, O_FILL);
c3.set(65, 18, 4, cGREEN, MEDIUM_FILL);
c1.draw(); //draw circles
c2.draw();
c3.draw();
set_cursor_pos(1, 25); // lower left corner
system("PAUSE");
return 0;
}
13
// display lengths
cout << "\ndist1 = ";
dist1.showdist();
cout << "\ndist2 = ";
dist2.showdist();
cout << endl;
system("PAUSE");
return 0;
}
15
Constructors
We see that member functions can be used to give values
to the data items in an object.
Sometimes, however, its convenient if an object can
initialize itself when its first created, without requiring a
separate call to a member function.
Automatic initialization is carried out using a special
member function called a constructor.
A constructor is a member function that is executed
automatically whenever an object is created.
Constructor has the same name of class and it has no
return type.
16
// increment count
int get_count()
{ return count; }
// return count
};
17
c1.get_count();
c2.get_count();
c1.get_count();
c2.get_count();
increment c1
increment c2
increment c2
display again
18
(1/2)
(2/2)
Destructor
You might guess that another function is called
automatically when an object is destroyed. This is indeed
the case. Such a function is called a destructor.
A destructor also has the same name as the class name
but is preceded by a tilde (~) sign:
Like constructors, destructors do not have a return value.
They also take no arguments.
The most common use of destructors is to de-allocate
memory that was allocated for the object by the
constructor.
21
Using Destructor
// foo.cpp demonstrates destructor
#include <iostream>
using namespace std;
class Foo{
private:
int data;
public:
Foo() : data(0)
// constructor (same name as class)
{cout<< "Wakeup \n" ; }
// destructor (same name with tilde)
~Foo()
{cout<< "ByeBye \n" ; }
};
int main(){
Foo s1, s2;
// define two objects of class Foo
system( "PAUSE" );
// Foo *s3; s3 = new Foo; delete s3;
return 0;
22
}
23
25
args. constructor
args. constructor
from user
= dist1 + dist2
Constructors Overloading
Since there are now two explicit constructors with the
same name, Distance(), we say the constructor is
overloaded.
Which of the two constructors is executed when an
object is created depends on how many arguments are
used in the definition:
Distance length;
// calls first constructor
Distance width(11, 6.0);
// calls second constructor
27
Objects as Arguments
Since add_dist() is a member function of the Distance
class, it can access the private data in any object of class
Distance supplied to it as an argument, using names like
dist1.inches and dist2.feet.
In the following statement dist3.add_dist(dist1, dist2);
add_dist() can access dist3, the object for which it was
called, it can also access dist1 and dist2, because they are
supplied as arguments.
When the variables feet and inches are referred to within
this function, they refer to dist3.feet and dist3.inches.
Notice that the result is not returned by the function. The
return type of add_dist() is void.
29
Objects as Arguments
The result is stored automatically in the dist3 object.
To summarize, every call to a member function is
associated with a particular object (unless its a static
function; well get to that later).
Using the member names alone (feet and inches), the
function has direct access to all the members, whether
private or public, of that object.
member functions also have indirect access, using the
object name and the member name, connected with the
dot operator (dist1.inches or dist2.feet) to other objects
of the same class that are passed as arguments.
30
22
2.25
10
11
6.25
31
//
//
//
//
37
39
22
2.25
10
11
6.25
40
44
46
(1/2)
(2/2)
int main()
{
Car Toyota,
cout << Toyota.How_Many() << " Cars are in Race" << endl;
Car Honda, Suzuki;
// create three objects
cout <<
// each
cout <<
cout <<
return 0;
}
48
};
50