Sie sind auf Seite 1von 13

Object oriented Programming

Passing objects to a function

Object oriented programming in C++ by Robert Lafore 1


Constructor
• A constructor will have exact same name as the class and it does not have any
return type at all, not even void.
• A constructor is a member function that is executed automatically whenever an
object is created.
• Constructors can be very useful for setting initial values for certain member
variables.
• A constructor is a special method of a class or structure in object-oriented
programming that initializes an object of that type.
• Same name as the class + no return type!
• Recommendation: Logic involving specific operations that need to be executed at
a particular event in an application - such as opening a database connection -
should not be written in a constructor

Object oriented programming in C++ by Robert Lafore 2


Counter example..
Scenario: count of number of customers entertained during each hour from 8am to 12pm
class Counter
{
private:
int count;
public:
Counter() Counter(): count(0)
{ {
count = 0;
} }
void inc_count() int main()
{
count++; {
} Counter slot1;
int get_count() slot1.inc_count(); //increment
{
return count; cout <<slot1.get_count();
} }
};

Object oriented programming in C++ by Robert Lafore 3


Overloaded constructors: Default vs parametrized constructors
• If you don’t specify parametrized constructor then default constructor is
automatically called by default.
class Counter int main()
{ void inc_count() {
private: {
int count; count++; Counter slot1;
public: }
int get_count()
slot1.inc_count(); //increment
Counter() cout << slot1.get_count();
{ {
cout << "default" << endl; return count;
} } Counter slot2(0);
Counter(int c) }; slot2.inc_count(); //increment
{ cout << slot2.get_count();
cout << “parametrized" << endl;
count = c; return 0;
} }
void zero_count()
{
count = 0;
}

Object oriented programming in C++ by Robert Lafore 4


Member Functions Defined Outside the Class
• It is only declared inside the class, with the statement
• void add_dist_feet( int );
• This tells the compiler that this function is a member of the class but that it will be defined
outside the class declaration

class Distance void getdist() void Distance::add_dist_feet(int val)


{ { {
private: cin >> feet; inches = inches+val;
}
int feet; cin >> inches;
float inches; }
public: void showdist() //display distance
Distance() { cout << feet << “ ”<< inches << ‘\”’;
{ feet=0; inches=0.0; } }
void add_dist_feet( int ); //declaration
Distance(int ft, float in) };
{ feet=ft; inches=in; }

Object oriented programming in C++ by Robert Lafore 5


int main()
{
Distance dist1(2,2.2), dist3;
Distance dist2(1, 6.25);
dist1.add_dist_feet(4);

dist1.showdist();
dist2.showdist();
cout << endl;
return 0;
}

Object oriented programming in C++ by Robert Lafore 6


Objects passed to a function
• It is only declared inside the class, with the statement
• void add_dist( Distance, Distance );
• This tells the compiler that this function is a member of the class but that it will be defined
outside the class declaration

class Distance void getdist() void Distance::add_dist(Distance d2, Distance d3)


{ { {
private: cin >> feet; inches = d2.inches + d3.inches;
feet += d2.feet + d3.feet;
int feet; cin >> inches; }
float inches; }
public: void showdist() //display distance
Distance() { cout << feet << “ ”<< inches << ‘\”’;
{ feet=0; inches=0.0; } }
void add_dist( Distance, Distance ); //declaration
Distance(int ft, float in) };
{ feet=ft; inches=in; }

Object oriented programming in C++ by Robert Lafore 7


void Distance::add_dist(Distance d2, Distance d3)
{
inches = d2.inches + d3.inches;
feet += d2.feet + d3.feet;
}

Object oriented programming in C++ by Robert Lafore 8


int main() void Distance::add_dist(Distance d2, Distance d3)
{ {
Distance dist1(2,2.2), dist3; inches = d2.inches + d3.inches;
Distance dist2(1, 6.25); feet = d2.feet + d3.feet;
dist3.add_dist(dist1, dist2);
}
//dist3 = dist1 + dist2

dist1.showdist();
dist2.showdist();
dist3.showdist();
cout << endl;
return 0;
}

Object oriented programming in C++ by Robert Lafore 9


Returning objects
Distance Distance::add_dist(Distance d2) int main()
{ {
Distance dist1, dist3;
Distance temp;
Distance dist2(11, 6.25);
temp.inches = inches + d2.inches; dist1.getdist();
temp.feet += feet + d2.feet; dist3 = dist1.add_dist(dist2);
return temp; //display all lengths
} cout << “\ndist1 = “; dist1.showdist();
cout << “\ndist2 = “; dist2.showdist();
cout << “\ndist3 = “; dist3.showdist();
cout << endl;
return 0;
}

Object oriented programming in C++ by Robert Lafore 10


Destructors
• A destructor has the same name as the class
• to deallocate memory that was allocated for the object by the constructor
• At the end of the program it is called as many times as the number of objects created of that class
class Foo
{
private:
int data;
public:
Foo() : data(0) //constructor
{}
~Foo() //destructor
{}
};

Object oriented programming in C++ by Robert Lafore 11


Array of objects (Dynamic vs static array declaration)
int main() int main()
{ {
int size; Distance parr[2];
size = 4; for(int i = 0; i < 2; i++)
Distance* p; {
p = new Distance[size]; cin >> parr[i].feet;
For (int i=0;i<size;i++) }
{ return 0;
cin>>p[i].feet; }
cin >> p[i].inches;
}
return 0;
}

Object oriented programming in C++ by Robert Lafore 12


That’s it

Object oriented programming in C++ by Robert Lafore 13

Das könnte Ihnen auch gefallen