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

219 Assignment #3 OOP

Uploaded by

ranaalam45171
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)
24 views

219 Assignment #3 OOP

Uploaded by

ranaalam45171
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/ 14

Chapter #9 : Inheritance

ASSIGNMENT # 3

CSC241-OBJECT ORIENTED PROGRAMMING (OOP)

SUBMITTED BY:

DANISH RIASAT

SUBMITTED TO:

MR. ALI USMAN

REGISTRATION NO.

FA22-BCS-219

Department of Computer Science

COMSATS University Islamabad, Sahiwal Campus


Question #1
#include <iostream> void getdata(void)
#include <string> {
#include <conio.h> publication::getdata(); //call publication class
using namespace std; function to get data
class publication cout << "Enter Book Page Count: "; //Acquire
{ book data from user
private: cin >> pagecount;
string title; }
float price; void putdata(void)
public: {
void getdata(void) publication::putdata(); //Show Publication data
{ cout << "Book page count: " << pagecount <<
string t; endl; //Show book data
float p; }
cout << "Enter title of publication: "; };
cin >> t; class tape :public publication
cout << "Enter price of publication:$ "; {
cin >> p; private:
title = t; float ptime;
price = p; public:
} void getdata(void)
void putdata(void) {
{ publication::getdata();
cout << "Publication title: " << title << endl; cout << "Enter tap's playing time(in minutes):
cout << "Publication price:$ " << price<<endl; ";
} cin >> ptime;
}; }
class book :public publication void putdata(void)
{ {
private: publication::putdata();
int pagecount; cout << "Tap's playing time(in minutes): " <<
public: ptime << endl;
}
};
int main()
{
book b;
tape t;
b.getdata();
t.getdata();
b.putdata();
t.putdata();
}
Output

Question #3
#include <iostream> class book :public publication,public sales
#include <string> {
#include <conio.h> private:
using namespace std; int pagecount;
class publication public:
{ void getdata(void)
private: {
string title; publication::getdata();
float price; sales::getdata();
public: cout << "Enter Book Page Count: ";
void getdata(void) cin >> pagecount;
{ }
string t; void putdata(void)
float p; {
cout << "Enter title of publication: "; publication::putdata();
cin >> t; sales::putdata();
cout << "Enter price of publication:$ "; cout << "Book page count: " << pagecount <<
cin >> p; endl;
title = t; }
price = p; };
} class tape :public publication,public sales
void putdata(void) {
{ private:
cout << "Publication title: " << title << endl; float ptime;
cout << "Publication price:$ " << price << public:
endl; void getdata(void)
} {
}; publication::getdata();
class sales sales::getdata();
{ cout << "Enter tap's playing time(in minutes):
private: ";
float s1, s2, s3; cin >> ptime;
public: }
void getdata(void) void putdata(void)
{ {
cout << "Enter month 1 sale: $"; publication::putdata();
cin >> s1; sales::putdata();
cout << "Enter month 2 sale: $"; cout << "Tap's playing time(in minutes): " <<
cin >> s2; ptime << endl;
cout << "Enter month 3 sale: $"; }
cin >> s3; };
} int main(void)
void putdata(void) {
{ book b;
cout << "Month 1 sale: $" << s1 << endl; tape t;
cout << "Month 2 sale: $" << s2 << endl; b.getdata();
cout << "Month 3 sale: $" << s3 << endl; t.getdata();
} b.putdata();
}; t.putdata();
}

Output
Question #4
#include <iostream> void putdata(void)
#include <string> {
#include <conio.h> publication::putdata();
using namespace std; sales::putdata();
class publication cout << "Book page count: " << pagecount <<
{ endl;
private: }
string title; };
float price; class tape :public publication, public sales
public: {
void getdata(void) private:
{ float ptime;
string t; public:
float p; void getdata(void)
cout << "Enter title of publication: "; {
cin >> t; publication::getdata();
cout << "Enter price of publication:$ "; sales::getdata();
cin >> p; cout << "Enter tap's playing time: ";
title = t; cin >> ptime;
price = p; }
} void putdata(void)
void putdata(void) {
{ publication::putdata();
cout << "Publication title: " << title << endl; sales::putdata();
cout << "Publication price:$ " << price << cout << "Tap's playing time: " << ptime <<
endl; endl;
} }
}; };
class sales class disk :public publication
{ {
private: private:
float s1, s2, s3; enum dtype
public: {CD,DVD};
void getdata(void) dtype userchoice;
{ public:
cout << "Enter month 1 sale: $"; void getdata(void)
cin >> s1; {
cout << "Enter month 2 sale: $"; char a;
cin >> s2; publication::getdata();
cout << "Enter month 3 sale: $"; cout << "Enter disk type (c or d) for CD and
cin >> s3; DVD: ";
} cin >> a;
void putdata(void) if (a == 'c')
{ userchoice = CD;
cout << "Month 1 sale: $" << s1 << endl; else
cout << "Month 2 sale: $" << s2 << endl; userchoice = DVD;
cout << "Month 3 sale: $" << s3 << endl;
} }
}; void putdata()
class book :public publication, public sales {
{ publication::putdata();
private: cout << "Disk type is: ";
int pagecount; if (userchoice == CD)
public: cout << "CD";
void getdata(void) else
{ cout << "DVD";
publication::getdata(); }
sales::getdata(); };
cout << "Enter Book Page Count: "; int main(void)
cin >> pagecount; {
} disk d;
d.getdata();
d.putdata();
}

Output

Question #5
#include <iostream> class manager : public employee2 //management
#include <conio.h> class
using namespace std; {
const int LEN = 80; //maximum length of names private:
class employee //employee class char title[LEN]; //"vice-president" etc.
{ double dues; //golf club dues
private: public:
char name[LEN]; //employee name void getdata()
unsigned long number; //employee number {
public: employee2::getdata();
void getdata() cout << " Enter title : "; cin >> title;
{ cout << " Enter golf club dues : "; cin >> dues;
cout << "\n Enter last name : "; cin >> name; }
cout << " Enter number :"; cin >> number; void putdata() const
} {
void putdata() const employee2::putdata();
{ cout << "\n Title : " << title;
cout << "\n Name : " << name; cout << "\n Golf club dues : " << dues;
cout << "\n Number : " << number; }
} };
};
class employee2 :public employee class scientist : public employee2 //scientist
{ class
private: {
double compen; private:
enum paytype{ hourly, weakly, monthly }; int pubs; //number of publications
paytype period; public:
public: void getdata()
void getdata(void) {
{ employee2::getdata();
char a; cout << " Enter number of pubs : "; cin >>
employee::getdata(); pubs;
cout << "Enter compensation: "; }
cin >> compen; void putdata() const
cout << "Enter payment period (h,w,m): "; {
cin >> a; employee2::putdata();
switch (a) cout << "\n Number of publications : " <<
{ pubs;
case 'h': }
period = hourly; };
break; class laborer : public employee2 //laborer class
case 'w': {};
period = weakly; int main(void)
break; {
case 'm': manager m1, m2;
period = monthly; scientist s1;
break; laborer l1;
} cout << endl; //get data for several employees
} cout << "\nEnter data for manager 1";
void putdata(void) const m1.getdata();
{ cout << "\nEnter data for manager 2";
employee::putdata(); m2.getdata();
cout << "Employee compensation: " << cout << "\nEnter data for scientist 1";
compen << endl; s1.getdata();
switch (period) cout << "\nEnter data for laborer 1";
{ l1.getdata();
case hourly: //display data for several employees
cout << "hourly" << endl; cout << "\nData on manager 1";
break; m1.putdata();
case weakly: cout << "\nData on manager 2";
cout << "weakly" << endl; m2.putdata();
break; cout << "\nData on scientist 1";
case monthly: s1.putdata();
cout << "monthly" << endl; cout << "\nData on laborer 1";
break; l1.putdata();
} cout << endl;
} _getch();
}; }
Output

Question #6
#include <iostream> if ((b - a) > LIMIT)
#include <conio.h> {
using namespace std; cout << "Array limits exceed maximum
#include <process.h> //for exit() permissible range."; exit(1);
const int LIMIT = 100; //array size }
class safearay }
{ int& operator [](int n)
private: {
int arr[LIMIT]; if (n < llimit || n >= ulimit)
public: {
int& operator [](int n) //note: return by cout << "\nIndex out of bounds"; _getch();
reference exit(1);
{ }
if (n< 0 || n >= LIMIT) safearay::operator[](n - llimit);
{ }
cout << "\nIndex out of bounds"; exit(1); };
} int main(void)
return arr[n]; {
} safehilo sa1(0,25);
}; for (int j = 0; j<25; j++) //insert elements
class safehilo :public safearay sa1[j] = j * 10; //*left* side of equal sign
{ for (int j = 0; j<25; j++) //display elements
private: {
int llimit, ulimit; int temp = sa1[j]; //*right* side of equal sign
public: cout << "Element " << j << " is " << temp <<
safehilo(int a, int b) :llimit(a), ulimit(b) endl;
{ }
_getch();
}

Output

Question #7
#include <iostream> // Function to decrement the count using
using namespace std; postfix notation
class Counter { void decrement() {
protected: count--;
int count; }
public: };
Counter() : count(20) {}
int main() {
// Function to display the current count // Example usage with prefix notation
void display() const { PrefixCounter prefixCounter;
cout << "Count: " << count << endl; prefixCounter.increment();
} prefixCounter.increment();
}; prefixCounter.increment();
prefixCounter.increment();
// Derived class for prefix notation prefixCounter.increment();
class PrefixCounter : public Counter { prefixCounter.increment();
public: prefixCounter.increment();
// Function to increment the count using cout<<"Prefix Increment ";
prefix notation prefixCounter.display();
void increment() { prefixCounter.decrement();
++count; cout<<"Prefix Decrement ";
} prefixCounter.display();

// Function to decrement the count using // Example usage with postfix notation
prefix notation PostfixCounter postfixCounter;
void decrement() { postfixCounter.increment();
--count; postfixCounter.increment();
} postfixCounter.increment();
}; postfixCounter.increment();
postfixCounter.increment();
// Derived class for postfix notation
class PostfixCounter : public Counter { cout<<"Postfix Increment ";
public: postfixCounter.display();
// Function to increment the count using
postfix notation postfixCounter.decrement();
void increment() { postfixCounter.decrement();
count++; cout<<"Postfix Decrement ";
} postfixCounter.display();
return 0;
}

Output

Question #9
#include <iostream> void getdata(void)
#include <string> {
#include <conio.h> publication2::getdata(); // Call publication2
using namespace std; class function to get data
cout << "Enter Book Page Count: "; //
class date Acquire book data from the user
{ cin >> pagecount;
private: }
int month;
int day; void putdata(void)
int year; {
publication2::putdata(); // Show
public: Publication data
void getdate() cout << "Book page count: " << pagecount
{ << endl; // Show book data
cout << "Enter date (mm dd yyyy): "; }
cin >> month >> day >> year; };
}
class tape : public publication2 // Inherit from
void showdate() publication2
{ {
cout << "Publication date: " << month << private:
"/" << day << "/" << year << endl; float ptime;
}
}; public:
void getdata(void)
class publication2 {
{ publication2::getdata();
private: cout << "Enter tape's playing time (in
string title; minutes): ";
float price; cin >> ptime;
date pubDate; // Adding date member }

public: void putdata(void)


void getdata(void) {
{ publication2::putdata();
cout << "Enter title of publication: "; cout << "Tape's playing time (in minutes):
cin >> title; " << ptime << endl;
cout << "Enter price of publication: $"; }
cin >> price; };
pubDate.getdate(); // Input date
} int main()
{
void putdata(void) book b;
{ tape t;
cout << "Publication title: " << title << b.getdata();
endl; t.getdata();
cout << "Publication price: $" << price << b.putdata();
endl; t.putdata();
pubDate.showdate(); // Output date // _getch();
} return 0;
}; }

class book : public publication2 // Inherit from


publication2
{
private:
int pagecount;

public:
Output

Question #10
#include <iostream> void inputExecutiveData() {
#include <string> inputManagerData(); // Call base class
using namespace std; function to input common data
class Manager { cout << "Enter yearly bonus: ";
private: cin >> bonus;
string name; cout << "Enter number of stock options: ";
int employeeId; cin >> stockOptions;
double salary; }

public: void displayExecutiveData() const {


void inputManagerData() { displayManagerData(); // Call base class
cout << "Enter manager name: "; function to display common data
cin >> name; cout << "Yearly Bonus: " << bonus <<
cout << "Enter employee ID: "; endl;
cin >> employeeId; cout << "Stock Options: " << stockOptions
cout << "Enter manager salary: "; << endl;
cin >> salary; }
} };

void displayManagerData() const { int main() {


cout << "Manager Name: " << name << Executive executive;
endl; executive.inputExecutiveData(); // Input data
cout << "Employee ID: " << employeeId for Executive
<< endl; cout << "\nExecutive Information:\n";
cout << "Salary: " << salary << endl;
} executive.displayExecutiveData(); // Display
}; data for Executive

class Executive : public Manager { return 0;


private: }
double bonus;
int stockOptions;

public:

Output

Question #11
#include <iostream> // Overloaded pop function for PairStack
using namespace std; Pair pop() {
Pair result;
// Define the pair structure
struct Pair { // Pop the y component first
int x; result.y = Stack2::pop();
int y;
}; // Pop the x component
result.x = Stack2::pop();
// Define the Stack2 class
class Stack2 { return result;
private: }
static const int MAX = 10; };
int st[MAX];
int top; int main() {
PairStack pairStack;
public:
Stack2() : top(-1) {} // Example usage
Pair myPair = {3, 7};
void push(int var) { pairStack.push(myPair);
st[++top] = var;
} // Pop the pair from the stack
Pair poppedPair = pairStack.pop();
int pop() {
return st[top--]; // Display the popped pair
} cout << "Popped Pair: (" << poppedPair.x <<
}; ", " << poppedPair.y << ")" << endl;

// Define the pairStack class derived from return 0;


Stack2 }
class PairStack : public Stack2 {
public:
// Overloaded push function for PairStack
void push(const Pair& p) {
// Push the x and y components onto the
stack
Stack2::push(p.x);
Stack2::push(p.y);
}

Output

You might also like