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)
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)
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