Lab 10 Oop
Lab 10 Oop
Task 1:
Write a class LocalPhone that contains an attribute phone to a local telephone number. The class
contains member accessor and mutator function for private data members. Write a child class
NatPhone for national phone numbers that inherits LocalPhone class. It additionally contains an
attribute to store city code. It also contains member functions to input and show the city code.
Write another class IntPhone for international phone numbers that inherits NatPhone class. It
additionally contains an attribute to store country code. It also contains setter and getter function for
private data members.
ANSWER:
#include<iostream>
using namespace std;
class localphone {
int number ;
public :
void get(int a){
number = a;
}
int set(){
return number;}
};
};
int main(){
int a;
cout<<"Enter Number :";
cin>>a;
localphone obj;
obj.get(a);
obj.set();
cout<<"your number is : "<<obj.set()<<endl;
natPhone obj1;
obj1.setcity();
intphone obj2;
obj2.setint();
return 0;
}
#include<iostream>
using namespace std;
class localphone {
int number ;
public :
void get(int a){
number = a;
}
int set(){
return number;}
};
int main(){
int a;
cout<<"Enter Number :";
cin>>a;
localphone obj;
obj.get(a);
obj.set();
cout<<"your number is : "<<obj.set()<<endl;
natPhone obj1;
obj1.setcity();
intphone obj2;
obj2.setint();
return 0;
}
Task 2:
Write a program in which class A is base class for class B. While Class C is derived from class
B and class D is derived from class C. Provide explicit implementation of default constructors
and destructors. Write a test program which initializes each class object and shows execution
order of constructor and destructor by printing appropriate messages.
ANSWER:
#include <iostream>
#include<string>
using namespace std;
class A{
string name ;
public:
void set(string a){
name = a ;
}
string get(){
return name;
}
};
class B : public A{
string father;
public :
string ss(){
};
class C : public B {
int age ;
public :
void ages (){
int a;
cout<<"\nEnter your age : ";
cin>>a;
age = a;
cout<<"My age is "<<age<<get()<<ss();
}
int gg(){
return age;
}
};
class D : public C {
int clas ;
public :
};
int main(){
string a;
cout<<"Enter your name : ";
cin>>a;
A obj;
obj.set(a);
cout<<"MY name is : "<<obj.get();
B obj2;
obj2.fathers();
obj2.ss();
C obj3;
obj3.ages();
obj3.gg();
D obj4 ;
obj4.claas();
return 0;
}
Task 3:
Modify the above classes and provide overloaded constructor implementation for each class.
(Note: Use base initialize for this purpose)
class A{
public:
A(int a) {
cout<< a<<endl;
}
};
class B : public A{
public :
B(int b) : A(5) {
cout<< b <<endl;
}
};
class C : public B {
public :
C(int c) : B(55) {
cout<< c <<endl;
}
};
class D : public C {
public :
D(int c) : C(555) {
cout<< c <<endl;
}
};
int main(){
D obj(5555) ;
return 0;
}
Task 4:
Declare classes Person and Student where Student is derived from Person class. Parent has
Name and Student has RollNo as its private member.
a) Create a Student class object and initialize it with the already existing object of Student
class. Analyze the behavior of default copy constructors of Person and Student classes.
b) Explicitly write copy constructor for Person class. Analyze the behavior.
c) Now, explicitly write copy constructor for the Student class.
#include <iostream>
#include<string>
using namespace std;
public:
void set(string a){
name = a ;
}
string get(){
return name;
}
};
class B : public A{
string father;
public :
string ss(){
return father;
}
};
class C : public B {
int age ;
public :
void ages (){
int a;
cout<<"\nEnter your age : ";
cin>>a;
age = a;
cout<<"My age is "<<age<<get()<<ss();
}
int gg(){
return age;
}
};
class D : public C {
int clas ;
public :
int main(){
string a;
cout<<"Enter your name : ";
cin>>a;
A obj;
obj.set(a);
cout<<"MY name is : "<<obj.get();
B obj2;
obj2.fathers();
obj2.ss();
C obj3;
obj3.ages();
obj3.gg();
D obj4 ;
obj4.claas();
return 0;
}
Total 40 Signature
Note : Attempt all tasks and get them checked by your Lab Instructor.