Sheet-5 Answers (Єℓ - ɒя) OOP
Sheet-5 Answers (Єℓ - ɒя) OOP
[1] By using classes, write a C++ program to calculate the area of Square,
Rectangle, and Triangle. Create three objects (sqr, rect, and tr) of types
(Square, Rectangle, and Triangle) respectively, then input the required
dimensions for each object from the keyboard, and finally display the
area of each object on the screen.
Ans.
#include <iostream>
using namespace std;
class Square {
private:
float side ;
public:
Square () {
cout << "Enter the side length of square: " ;
cin >> side ;
}
double getarea(){
return side * side ;
}
};
class Rectangle {
private:
float length , width ;
public:
Rectangle() {
cout << "Enter the length and width of rectangle: " ;
cin >> length >> width ;
}
1
By : єℓ_ɒя
double getarea(){
return length * width ;
}
};
class Triangle {
private:
float base , height ;
public:
Triangle() {
cout << "Enter the base and height of triangle: " ;
cin >> base >> height ;
}
double getarea(){
return (0.5)* base * height ;
}
};
int main()
{
Square sqr;
Rectangle rec;
Triangle tr;
return 0;
}
2
By : єℓ_ɒя
[2] By using classes, write a C++ program to simulate simple calculator that
performs the 4 mathematical operations addition, subtraction,
multiplication and division of two numbers.
Ans.
#include <iostream>
using namespace std;
class calculator {
private:
float num1, num2;
public:
calculator() {
cout << "Enter two numbers: ";
cin >> num1 >> num2;
}
double add() {
return num1 + num2;
}
double sub() {
return num1 - num2;
}
double multiply() {
return num1 * num2;
}
double divide() {
if (num2 == 0) {
cout << "Error! Division by zero." << endl;
} else {
int main() {
calculator cal;
3
By : єℓ_ɒя
cout << "- The Addition is: " << cal.add() << endl;
cout << "- The Subtraction is: " << cal.sub() << endl;
cout << "- The Multiplication is: " << cal.multiply() << endl;
cout << "- The Division is: " << cal.divide() << endl;
return 0;
}
Or
Ans.
#include <iostream>
using namespace std;
class calculator {
private:
float num1, num2;
public:
calculator() {
cout << "Enter two numbers: ";
cin >> num1 >> num2;
}
double add() {
return num1 + num2;
}
double sub() {
return num1 - num2;
}
double multiply() {
return num1 * num2;
}
double divide() {
if (num2 == 0) {
cout << "Error! Division by zero." << endl;
} else {
return num1 / num2;
4
By : єℓ_ɒя
}
}
};
int main() {
calculator cal;
int choose;
cout << "Enter 1 to addition ,2 to subtraction ,3 to multiplication or 4 to
Division: " ;
cin >> choose ;
switch (choose)
{
case 1:
cout << "-The Addition is: " << cal.add() << endl;
break;
case 2:
cout << "-The Subtraction is: " << cal.sub() << endl;
break;
case 3:
cout << "-The Multiplication is: " << cal.multiply() << endl;
break;
case 4:
cout << "-The Division is: " << cal.divide() << endl;
break;
default:
cout << "Invalid choice!,try again. " << endl;
}
return 0;
}
5
By : єℓ_ɒя
[3] Write an OOP complete program to represent a
Vehicle Using the following UML diagram as a guide.
Ans.
#include <iostream>
using namespace std;
class Vehicle {
private:
string Model;
string Color;
public:
int Registration;
Vehicle() {
cout << "-Enter the model and color of vehicle: " ;
cin >> Model >> Color;
cout << "-Enter the registration of vehicle: " ;
cin >> Registration;
void Forward(){
cout << "-The vehicle is moving forward. "<< endl;
}
void Backward(){
cout << "-The vehicle is moving backward. "<< endl;
}
void Stop(){
cout << "-The vehicle has stopped. "<< endl;
}
void displayinfo(){
cout << "- Model: "<< Model<<endl;
cout << "- Color: "<< Color<<endl;
cout << "- Registration: "<< Registration<<endl;
}
6
By : єℓ_ɒя
};
int main() {
Vehicle v1;
v1.displayinfo();
v1.Forward();
v1.Backward();
v1.Stop();
return 0;
}
Or
Ans.
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
private:
string model;
string color;
public:
int registration;
Vehicle() {
registration = 111;
}
void forward() {
cout << "Vehicle number: " << registration << " is moving forward." << endl;
}
void backward() {
cout << "Vehicle is moving backward." << endl;
}
void Stop() {
cout << "Vehicle has stopped." << endl;
}
};
7
By : єℓ_ɒя
int main() {
Vehicle v1;
v1.forward();
v1.registration = 333;
v1.forward();
v1.backward();
v1.Stop();
return 0;
}
8
By : єℓ_ɒя
[4] Write an OOP complete program to represent a car model, color and
price. The class constructor will set the values to all its data members.
Create a member function outside the class to display the car price, and
create a function to increase the car price by 1000. Create an object and
input its data from the keyboard, and finally display the object data.
Ans.
#include <iostream>
#include <string>
using namespace std;
class Car {
private:
string model;
string color;
double price;
public:
Car(string m, string c, double p) : model(m), color(c), price(p) {}
void displayPrice() ;
void increasePrice() {
price += 1000;
}
};
void Car::displayPrice(){
cout << "Car Model: " << model << "\n";
cout << "Car Color: " << color << "\n";
cout << "The car price is: $" << price << endl;
}
int main() {
string model, color;
double price;
9
By : єℓ_ɒя
cin >> price;
return 0;
}
10
By : єℓ_ɒя
[5] Create a class called time that has separate int member data for hours,
minutes, and seconds. One constructor should initialize this data to 0, and
another should initialize it to fixed values. Another member function
should display it, in 11:59:59 format. The final member function should
add two objects of type time passed as arguments.
A main() program should create two initialized time objects (should they
be const?) and one that isn‟t initialized. Then it should add the two
initialized values together, leaving the result in the third time variable.
Finally it should display the value of this third variable. Make appropriate
member functions const.
Ans.
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
int seconds;
public:
Time() : hours(0), minutes(0), seconds(0) {}
}
};
int main() {
const Time t1(5, 12, 30);
const Time t2(6, 30, 10);
11
By : єℓ_ɒя
Time t3;
t3.addTime(t1, t2);
t3.display();
return 0;
}
12
By : єℓ_ɒя