Oop Assignment 4 170
Oop Assignment 4 170
SAHIWAL CAMPUS
ASSIGNMENT # 4
Submitted by:
Husnain Raza
Submitted to:
Sir Fayez Afzaal
Registration no:
SP23-BCS-170
Date of submission:
15-MAY-2024
Course title:
OBJECT ORIENTED PROGRAMMING
(OOP)
QUESTION NO 1:
Write a class person that has attributes of id, name and address. It has a constructor to
initialize, a member function to input and a member function to display data members.
Create 2nd class Student that inherits Person class. It has additional attributes of roll
number and marks. It also has member functions to input and display its data members.
Create 3rd class Scholarship that inherits student class. It has additional attributes of
scholarship name and amount. It also has member functions to input and display its data
members.
SOLUTION:
#include <iostream>
#include <string>
Using namespace std;
Class Person {
Protected:
Int id;
String name;
String address;
Public:
Void display() {
Protected:
Int rollNumber;
Float marks;
Public:
Void input() {
Person::input();
Cout << “Enter Roll Number: “;
Cin >> rollNumber;
Cout << “Enter Marks: “;
Cin >> marks;
}
Void display() {
Person::display();
Cout << “Roll Number: “ << rollNumber << endl;
Cout << “Marks: “ << marks << endl;
}
};
Private:
String scholarshipName;
Float amount;
Public:
Void input() {
Student::input();
Cout << “Enter Scholarship Name: “;
Cin.ignore();
Getline(cin, scholarshipName);
Cout << “Enter Amount: “;
Cin >> amount;
}
Void display() {
Student::display();
Cout << “Scholarship Name: “ << scholarshipName << endl;
Cout << “Amount: “ << amount << endl;
}
};
Int main() {
Scholarship s(0, “”, “”, 0, 0.0, “”, 0.0);
s.input();
cout << “Scholarship Details:” << endl;
s.display();
return 0;
}
Question no #2
Write a class LocalPhone that contains an attribute phone to store a local telephone
number. The class contains member functions to input and display phone number. Write
a child class NatPhone for national phone numbers that inherits LocalPhone class. It
additionaly contains an attribute to store city code. It also contains member function 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 member functions to input and show the country code.
SOLUTION:
#include <iostream>
#include <string>
using namespace std;
class LocalPhone {
protected:
string phone;
public:
void input() {
cout << "Enter local phone number: ";
cin >> phone;
}
void display() {
cout << "Local Phone Number: " << phone << endl;
}
};
class NatPhone : public LocalPhone {
protected:
string cityCode;
public:
void input() {
LocalPhone::input();
cout << "Enter city code: ";
cin >> cityCode;
}
void display() {
LocalPhone::display();
cout << "City Code: " << cityCode << endl;
}
};
protected:
string countryCode;
public:
void input() {
NatPhone::input();
cout << "Enter country code: ";
cin >> countryCode;
}
void display() {
NatPhone::display();
cout << "Country Code: " << countryCode << endl;
}
};
int main() {
IntPhone phone;
phone.input();
cout << "Phone Details:" << endl;
phone.display();
return 0;
}
Question no #3
Define data encapsulation and explain it with example.
SOLUTION:
Data encapsulation :
Data encapsulation is one of the fundamental principles of object-oriented programming
(OOP). It refers to the bundling of data (attributes) and the methods (functions) that
operate
on that data within a single unit, typically a class. Encapsulation hides the internal state of
an object from the outside world and only exposes a controlled interface to interact with
the object’s data. This helps in achieving data abstraction and information hiding, making
the implementation details of a class inaccessible to the user.
Example :
#include <iostream>
#include <string>
Using namespace std;
Class Rectangle {
Private:
Double length;
Double width;
Public:
Void setLength(double l) {
If (l > 0)
Length = l;
}
Void setWidth(double w) {
If (w > 0)
Width = w;
}
Double getLength() {
Return length;
}
Double getWidth() {
Return width;
}
Double calculateArea() {
Return length * width;
}
};
Int main() {
Rectangle r1;
R1.setLength(5.0);
R1.setWidth(3.0);
Question no #4
Define association and explain it with example.
SOLUTION:
Association :
Association is a relationship between two or more classes in which objects of one class
are
connected to objects of another class. It represents the “has-a” relationship between
objects, where one object uses another object to perform some functionality or to
represent some attribute.
Example :
#include <iostream>
#include <string>
Using namespace std;
Class Car {
Public:
String model;
Public:
String name;
Car* car;
Person(string _name) : name(_name), car(nullptr) {}
Void buyCar(Car& _car) {
Car = &_car;
}
Void display() {
Cout << “Name: “ << name << endl;
If (car != nullptr) {
Cout << “Car Model: “ << car->model << endl;
} else {
Cout << “No car owned.” << endl;
}
}
};
Int main() {
Person person(“John”);
Car car(“Toyota Corolla”);
Person.buyCar(car);
Person.display();
Return 0;
}