0% found this document useful (0 votes)
4 views4 pages

Mocks: Employee Manager

Uploaded by

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

Mocks: Employee Manager

Uploaded by

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

Question 1: Question 2:

Write a class Mocks with the data members to store the three Write a C++ program implementing
mocks, write three members function into input numbers, sum to
calculate and return the sum, and average to calculate and return
inheritance between Employee basic class
the average mocks. and Manager derived class.
Answer: Answer:
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class Mocks {
int m1, m2, m3; class Employee {
protected:
public: string name;
void input() { int id;
cout << "Enter three mocks: ";
cin >> m1 >> m2 >> m3;
} public:
int sum() { void getData() {
return m1 + m2 + m3; cout << "Enter Employee Name and ID: ";
} cin >> name >> id;
float average() {
return (m1 + m2 + m3) / 3.0;
}
} void display() {
}; cout << "Employee Name: " << name << ",
ID: " << id << endl;
int main() { }
Mocks obj;
obj.input();
};
cout << "Sum = " << obj.sum() << endl;
cout << "Average = " << obj.average() << endl; class Manager : public Employee {
} string department;
Question 3:
Diagrammatically represent the following scenario of inheritance:
public:
Shape as a base class, Circle, Rectangle, Triangle, and Square as void getManagerData() {
derived. getData();
Answer: cout << "Enter Department: ";
Shape cin >> department;
/| | \
/ | | \
}
Circle Rectangle Triangle Square void showManager() {
This is hierarchical inheritance (one base → multiple derived). display();
Question 4: cout << "Department: " << department <<
Write a class that displays a simple message on the screen endl;
whenever an object of that class is created.
Answer:
}
#include <iostream> };
using namespace std;
int main() {
class Message { Manager m;
public:
Message() {
m.getManagerData();
cout << "Object Created Successfully!" << endl; m.showManager();
} }
};

int main() {
Message m1; // constructor runs automatically Question 7:
} Explain inheritance and polymorphism with
Question 5:
examples.
Write a C++ program implementing a class with the name Time. The Answer:
class should have a constructor to initialize the time (hours, Inheritance
minutes, seconds). The class should have another function named o Mechanism of creating a new
toSeconds to convert and display the time into seconds. class (derived) from an existing
Answer:
#include <iostream>
class (base).
using namespace std; o Example:
class Animal {
class Time { public:
int h, m, s; void sound() { cout << "Animals make
public:
sounds" << endl; }
Time(int hours, int minutes, int seconds) { };
h = hours;
m = minutes; class Dog : public Animal {
s = seconds; public:
}
int toSeconds() {
void sound() { cout << "Dog barks" <<
return (h * 3600) + (m * 60) + s; endl; }
} };
}; Polymorphism
o Ability of the same function to
int main() {
Time t(1, 30, 45);
behave di erently in di erent
cout << "Time in seconds = " << t.toSeconds() << endl; classes.
} o Two types: Compile-time
Question 6: (function overloading, operator
What is an object, how are objects created to access member of a overloading) and Runtime
class?
Answer:
(virtual functions).
 An object is an instance of a class, representing real-world o Example:
entities. class Shape {
 It is used to access class data members and member public:
functions. virtual void draw() { cout << "Drawing
Example:
Shape" << endl; }
class Student {
public: };
string name;
void display() { class Circle : public Shape {
cout << "Name: " << name << endl; public:
}
void draw() override { cout <<
};
"Drawing Circle" << endl; }
int main() { };
Student s; // object creation
s.name = "Ali"; // access data member int main() {
s.display(); // access member function
Shape *s;
}
Circle c;
s = &c;
s->draw(); // runtime polymorphism
}

You might also like