Assignment 2: Object Orientated Programing
Assignment 2: Object Orientated Programing
2
Object Orientated
Programing
Ijaz Ahmad
Roll No . 021
Q. 1
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class publication
{
private:
string title;
float price;
public:
void getdata(void)
{
string t;
float p;
cout << "Enter title of publication: ";
cin >> t;
cout << "Enter price of publication: ";
cin >> p;
title = t;
price = p;
}
void putdata(void)
{
cout << "Publication title: " << title << endl;
cout << "Publication price: " << price<<endl;
}
};
class book :public publication
{
private:
int pagecount;
public:
void getdata(void)
{
publication::getdata(); //call publication class function to get data
cout << "Enter Book Page Count: "; //Acquire book data from user
cin >> pagecount;
}
void putdata(void)
{
publication::putdata(); //Show Publication data
cout << "Book page count: " << pagecount << endl; //Show book data
}
};
class tape :public publication
{
private:
float ptime;
public:
void getdata(void)
{
publication::getdata();
cout << "Enter tap's playing time: ";
cin >> ptime;
}
void putdata(void)
{
publication::putdata();
cout << "Tap's playing time: " << ptime << endl;
}
};
void main(void)
{
book b;
tape t;
b.getdata();
t.getdata();
b.putdata();
t.putdata();
_getch();
}
Q.2
#include <iostream>
#include <conio.h>
using namespace std;
class String
{
protected:
enum { SZ = 80 }; //max size of Strings
char str[SZ]; //array
public:
String() //constructor, no args
{
str[0] = '\0';
}
String(char s[]) //constructor, one arg
{
strcpy_s(str, s);
}
void display() //display string
{
cout << str;
}
void concat(String s2) //add arg string to
{ //this string
if (strlen(str) + strlen(s2.str) < SZ)
strcat_s(str, s2.str);
else
cout << "\nString too long";
}
};
class Pstring :public String
{
public:
Pstring()
{}
Pstring(char s[])
{
int len = strlen(s);
if (len < SZ)
strcpy_s(str, s);
else
{
for (int i = 0; i < (SZ); i++)
{
str[i] = s[i];
}
}
}
};
void main(void)
{
Pstring s, s1;
s = "Awais Irfan";
s1 = "This string will surely exceed the width of the screen, which is what the SZ
constant represents.";
s.display();
cout << endl;
s1.display();
cout << endl;
_getch();
}
Q. 3
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class publication
{
private:
string title;
float price;
public:
void getdata(void)
{
string t;
float p;
cout << "Enter title of publication: ";
cin >> t;
cout << "Enter price of publication: ";
cin >> p;
title = t;
price = p;
}
void putdata(void)
{
cout << "Publication title: " << title << endl;
cout << "Publication price: " << price << endl;
}
};
class sales
{
private:
float s1, s2, s3;
public:
void getdata(void)
{
cout << "Enter month 1 sale: $";
cin >> s1;
cout << "Enter month 2 sale: $";
cin >> s2;
cout << "Enter month 3 sale: $";
cin >> s3;
}
void putdata(void)
{
cout << "Month 1 sale: $" << s1 << endl;
cout << "Month 2 sale: $" << s2 << endl;
cout << "Month 3 sale: $" << s3 << endl;
}
};
class book :public publication,public sales
{
private:
int pagecount;
public:
void getdata(void)
{
publication::getdata();
sales::getdata();
cout << "Enter Book Page Count: ";
cin >> pagecount;
}
void putdata(void)
{
publication::putdata();
sales::putdata();
cout << "Book page count: " << pagecount << endl;
}
};
class tape :public publication,public sales
{
private:
float ptime;
public:
void getdata(void)
{
publication::getdata();
sales::getdata();
cout << "Enter tap's playing time: ";
cin >> ptime;
}
void putdata(void)
{
publication::putdata();
sales::putdata();
cout << "Tap's playing time: " << ptime << endl;
}
};
void main(void)
{
book b;
tape t;
b.getdata();
t.getdata();
b.putdata();
t.putdata();
_getch();
}
Q. 4
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class publication
{
private:
string title;
float price;
public:
void getdata(void)
{
string t;
float p;
cout << "Enter title of publication: ";
cin >> t;
cout << "Enter price of publication: ";
cin >> p;
title = t;
price = p;
}
void putdata(void)
{
cout << "Publication title: " << title << endl;
cout << "Publication price: " << price << endl;
}
};
class sales
{
private:
float s1, s2, s3;
public:
void getdata(void)
{
cout << "Enter month 1 sale: $";
cin >> s1;
cout << "Enter month 2 sale: $";
cin >> s2;
cout << "Enter month 3 sale: $";
cin >> s3;
}
void putdata(void)
{
cout << "Month 1 sale: $" << s1 << endl;
cout << "Month 2 sale: $" << s2 << endl;
cout << "Month 3 sale: $" << s3 << endl;
}
};
class book :public publication, public sales
{
private:
int pagecount;
public:
void getdata(void)
{
publication::getdata();
sales::getdata();
cout << "Enter Book Page Count: ";
cin >> pagecount;
}
void putdata(void)
{
publication::putdata();
sales::putdata();
cout << "Book page count: " << pagecount << endl;
}
};
class tape :public publication, public sales
{
private:
float ptime;
public:
void getdata(void)
{
publication::getdata();
sales::getdata();
cout << "Enter tap's playing time: ";
cin >> ptime;
}
void putdata(void)
{
publication::putdata();
sales::putdata();
cout << "Tap's playing time: " << ptime << endl;
}
};
class disk :public publication
{
private:
enum dtype
{CD,DVD};
dtype userchoice;
public:
void getdata(void)
{
char a;
publication::getdata();
cout << "Enter disk type (c or d) for CD and DVD: ";
cin >> a;
if (a == 'c')
userchoice = CD;
else
userchoice = DVD;
}
void putdata()
{
publication::putdata();
cout << "Disk type is: ";
if (userchoice == CD)
cout << "CD";
else
cout << "DVD";
}
};
void main(void)
{
disk d;
d.getdata();
d.putdata();
_getch();
}
Q. 5
#include <iostream>
#include <conio.h>
using namespace std;
const int LEN = 80; //maximum length of names
////////////////////////////////////////////////////////////////
class employee //employee class
{
private:
char name[LEN]; //employee name
unsigned long number; //employee number
public:
void getdata()
{
cout << "\n Enter last name : "; cin >> name;
cout << " Enter number :"; cin >> number;
}
void putdata() const
{
cout << "\n Name : " << name;
cout << "\n Number : " << number;
}
};
////////////////////////////////////////////////////////////////
class employee2 :public employee
{
private:
double compen;
enum paytype{ hourly, weakly, monthly };
paytype period;
public:
void getdata(void)
{
char a;
employee::getdata();
cout << "Enter compensation: ";
cin >> compen;
cout << "Enter payment period (h,w,m): ";
cin >> a;
switch (a)
{
case 'h':
period = hourly;
break;
case 'w':
period = weakly;
break;
case 'm':
period = monthly;
break;
}
}
void putdata(void) const
{
employee::putdata();
cout << "Employee compensation: " << compen << endl;
switch (period)
{
case hourly:
cout << "hourly" << endl;
break;
case weakly:
cout << "weakly" << endl;
break;
case monthly:
cout << "monthly" << endl;
break;
}
}
};
////////////////////////////////////////////////////////////////
class manager : public employee2 //management class
{
private:
char title[LEN]; //"vice-president" etc.
double dues; //golf club dues
public:
void getdata()
{
employee2::getdata();
cout << " Enter title : "; cin >> title;
cout << " Enter golf club dues : "; cin >> dues;
}
void putdata() const
{
employee2::putdata();
cout << "\n Title : " << title;
cout << "\n Golf club dues : " << dues;
}
};
////////////////////////////////////////////////////////////////
class scientist : public employee2 //scientist class
{
private:
int pubs; //number of publications
public:
void getdata()
{
employee2::getdata();
cout << " Enter number of pubs : "; cin >> pubs;
}
void putdata() const
{
employee2::putdata();
cout << "\n Number of publications : " << pubs;
}
};
////////////////////////////////////////////////////////////////
class laborer : public employee2 //laborer class
{};
////////////////////////////////////////////////////////////////
void main(void)
{
manager m1, m2;
scientist s1;
laborer l1;
cout << endl; //get data for several employees
cout << "\nEnter data for manager 1";
m1.getdata();
cout << "\nEnter data for manager 2";
m2.getdata();
cout << "\nEnter data for scientist 1";
s1.getdata();
cout << "\nEnter data for laborer 1";
l1.getdata();
//display data for several employees
cout << "\nData on manager 1";
m1.putdata();
cout << "\nData on manager 2";
m2.putdata();
cout << "\nData on scientist 1";
s1.putdata();
cout << "\nData on laborer 1";
l1.putdata();
cout << endl;
_getch();
}