0% found this document useful (0 votes)
115 views

Lab 10 Oop

The document discusses a lab assignment completed by a student. It includes 4 tasks involving object-oriented programming concepts like inheritance, constructors, and copy constructors. A grading sheet at the end indicates the student received full marks for completing all 4 tasks.

Uploaded by

HA Aim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

Lab 10 Oop

The document discusses a lab assignment completed by a student. It includes 4 tasks involving object-oriented programming concepts like inheritance, constructors, and copy constructors. A grading sheet at the end indicates the student received full marks for completing all 4 tasks.

Uploaded by

HA Aim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 10
(Spring 2018)

Course: Object Oriented Programming Lab Date:


Course Code: CSL - 210 Max Marks: 40
Faculty’s Name: Aasim Ali Lab Engineer: Shoaib Khan

Name: Mohammad Jasim Waqar Enroll No: 03-135172-055

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;}
};

class natPhone : public localphone {


int citycode;
public :
void setcity(){
int a;
cout<<"Enter city code :";
cin>>a;
cout<<a<<set()<<endl;
}
};

class intphone : public natPhone {

Department of Computer Science, BULC


int countrycode;

public :void setint(){


int a;
cout<<"Enter city code :";
cin>>a;
cout<<a<<set()<<endl;
}

};

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;}
};

class natPhone : public localphone {


int citycode;
public :
void setcity(){
int a;
cout<<"Enter city code :";
cin>>a;
cout<<a<<set()<<endl;
}
};

class intphone : public natPhone {


int countrycode;

public :void setint(){


int a;
cout<<"Enter city code :";
cin>>a;
cout<<a<<set()<<endl;
}

Department of Computer Science, BULC


};

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 :

void fathers (){


string a;
cout<<"\nEnter your father name : ";
cin>>a;
father = a;
cout<<get()<<father;
}

string ss(){

Department of Computer Science, BULC


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 :

string claas (){


int a;
cout<<"\nEnter your class : ";
cin>>a;
clas = a;
cout<<"My class is "<<clas;
return 0;
}

};

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)

Department of Computer Science, BULC


ANSWER: #include <iostream>
#include<string>
using namespace std;

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;

Department of Computer Science, BULC


class A{
string name ;

public:
void set(string a){
name = a ;
}

string get(){
return name;
}
};

class B : public A{
string father;
public :

void fathers (){


string a;
cout<<"\nEnter your father name : ";
cin>>a;
father = a;
cout<<get()<<father;
}

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 :

string claas (){


int a;
cout<<"\nEnter your class : ";
cin>>a;
clas = a;
cout<<"My class is "<<clas;
return 0;
}

Department of Computer Science, BULC


};

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;
}

Lab Grading Sheet :


Max Obtained
Task Comments(if any)
Marks Marks
1. 10
2. 10
3. 10
4. 10

Total 40 Signature

Note : Attempt all tasks and get them checked by your Lab Instructor.

Department of Computer Science, BULC

You might also like