Overloaded Constructors and Multiple Classes OOP Overloaded Constructors and Multiple Classes Malik Jahan Khan Malik Jahan Khan
OOP Overloaded Functions We may have many functions in the same program having same function name but different number of arguments They are differentiated by the number of arguments passed while calling them from main() Such functions are called overloaded functions Malik Jahan Khan
Constructors having Arguments Some times, we need to specify default initialization values at the time of object creation This can be done by passing arguments to constructors In this way, each object may be initialized through constructor with a different value
Example #include<iostream> using namespace std; class Person { private: int id; public: Person(int d) : id(d) } void showID() cout<<"\n My ID is "<<id; }; int main() Person p2(10),p1(20),p3(30); p1.showID(); p2.showID(); p3.showID(); Output My ID is 20 My ID is 10 My ID is 30
Overloaded Constructors There may be many constructors of a class, each having different number of arguments
Example Output Feet = 1.... Inches = 5 Feet = 5.... Inches = 9 #include<iostream> using namespace std; class Distance { private: int feet; float inches; public: Distance(): feet(1), inches(5.0) } Distance(int ft, float in): feet(ft), inches(in) void showDist() cout<<"\n Feet = "<<feet<<".... Inches = "<<inches; }; void main() Distance d1; Distance d2(5,9); d1.showDist(); d2.showDist(); Output Feet = 1.... Inches = 5 Feet = 5.... Inches = 9
Default Copy Constructor A no-argument constructor initializes data members with constant values A multi-argument constructor initializes data members to values passed as arguments We can also initialize an object with another object of same class Surprisingly, we don’t need to write a special constructor for it It is already built into all classes and known as “default copy constructor”
Example Output dist1 = 11'-6.25" dist2 = 11'-6.25" dist3 = 11'-6.25” #include <iostream> using namespace std; class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in): feet(ft), inches(in) void showdist() { cout << feet << "\'-" << inches << "\""; } }; int main() Distance dist1(11, 6.25); //two-arg constructor Distance dist2(dist1); //one-arg constructor Distance dist3 = dist1; //default copy constructor cout << "\ndist1 = "; dist1.showdist(); cout << "\ndist2 = "; dist2.showdist(); cout << "\ndist3 = "; dist3.showdist(); cout << endl; return 0; } Output dist1 = 11'-6.25" dist2 = 11'-6.25" dist3 = 11'-6.25”
Multiple Classes In real world scenarios, we may have deal with different classes in a single problem In an OO program, we may define as many classes as we need to represent a real world problem
Example OOP #include <iostream> #include<stdio.h> #include<cstring> using namespace std; class Car { public: int number; char color[10]; Car() { } Car(int n, char col[10]): number(n) { strcpy(color,col); } }; class Person private: int id; char name[10]; Car myCar; Person(): id(0) void setData(int i, char n[10], Car c) { id=i; strcpy(name,n); myCar=c; } void showData() cout<<"\n I am ID No. "<<id<<", my name is "<<name<<". I have car number "<<myCar.number<<" of color "<<myCar.color; }; int main() Person p1, p2, p3; Car c1(3439, "Red"),c2(7777, "Black"); p1.setData(1,“Tahira",c1); p2.setData(2,“Maryam",c2); p3.setData(3,“Kainat",c1); p1.showData(); p2.showData(); p3.showData(); cout<<"\n"; return 0; Malik Jahan Khan
Output I am ID No. 1, my name is Tahira. I have car number 3439 of color Red I am ID No. 2, my name is Maryam. I have car number 7777 of color Black I am ID No. 3, my name is Kainat. I have car number 3439 of color Red