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

Poly Mar Phi Sim

The document contains code for an object-oriented programming assignment that defines classes for publications including books and audiotapes. It creates a Publication base class that is derived into Book and Tape classes which add additional attributes. A main function then prompts the user to enter book or tape data, creates the objects and stores pointers to them in an array before outputting the data.

Uploaded by

Muhammed Ali
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)
40 views

Poly Mar Phi Sim

The document contains code for an object-oriented programming assignment that defines classes for publications including books and audiotapes. It creates a Publication base class that is derived into Book and Tape classes which add additional attributes. A main function then prompts the user to enter book or tape data, creates the objects and stores pointers to them in an array before outputting the data.

Uploaded by

Muhammed Ali
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/ 6

UNIVERSITY OF HARIPUR

Khyber Pakhtunkhwa, pakistan


2st Semester
Assignment # 04
SUBMITTED TO: MAM MISBAH SIKANDER
Year 2020

NAME ABEEHA ZAINAB


ROLL NO : S20-0154
SECTION ‘B’
SUBJECT OBJECT ORIENTED PROGRAMMING
DEPARTMENT INFORMATION TECHNOLOGY (IT)
Q1). Imagine a publishing company that makes both book and audiocassette versions of its
works. Create a class publication that stores the title (a string) and price (type float)of a
publication. From this class derive two classes: book, which adds a page count (type
int), and tape, which adds a playing time in minutes (type float). Each of these three classes
should have a getdata() function to get its data from the user at the keyboard, and a putdata()
function to display its data.
Write a main() program that creates an array of pointers to publication. In a loop, ask the user
for data about a particular book or tape, and use new to create an object of type book or tape to
hold the data. Put the pointer to the object in the array. When the user has finished entering the
data for all books and tapes, display the resulting data for all the books and tapes entered, using
a for loop and a single statement such as pubarr[j]->putdata(); to display the data from each
object in the array.

CODE :
include<iostream>
#include<string>
using namespace std;

class Publication
{
private:
string title;
float price;
public:
void getName()
{
cout<<“Enter Title: ”; cin>>title;
cout<<“Enter Price: $”; cin>>price;
}

void putName()
{
cout<<“\nTitle: ”<<title;
cout<<“, Price: $”<<price;
}

virtual void getData() = 0;


};

class Book : public Publication


{
private:
int pages;
public:
void getData()
{
Publication::getName();
cout<<“Enter Pages: ”; cin>>pages;
}

void putData()
{
Publication::putName();
cout<<“, Pages: ”<<pages<<endl;
}
};

class Tape : public Publication


{
private:
float minutes;
public:
void getData()
{
Publication::getName();
cout<<“Enter Minutes: ”; cin>>minutes;
}

void putData()
{
Publication::putName();
cout<<“, Minutes: ”<<minutes<<endl;
}
};

int main()
{
Publication* ptrPub[100];
int n = 0;
char choice;

do
{
cout<<“Book or Tape? (b/t): ”; cin>>choice;
if(choice == ‘b’)
{ ptrPub[n] = new Book; ptrPub[n]->getData(); }
else
{ ptrPub[n] = new Tape; ptrPub[n]->getData(); }
n++; cout<<“Enter another? (y/n): ”; cin>>choice;
} while(choice == 'y’);

for(int i=0; i<n; i++)


ptrPub[i]->putName();
cout<<endl;
return 0;
}

Output

CODE :
#include <iostream>
#include <string>
using namespace std;
class Creature{
public:
Creature() {}
Creature(string name);
virtual void DoAction()=0;
virtual void DrawOnScreen()=0;
protected:
string CreatureName;
};
class Player: public Creature {
public:
Player(string name);
void DoAction();
void DrawOnScreen();
};
class Monster: public Creature {
public:
Monster() {}
Monster(string name);
void DoAction();
void DrawOnScreen();
};
class WildPig: public Monster {
public:
WildPig(string name);
void DoAction();
void DrawOnScreen();
};
class Dragon: public Monster {
public:
Dragon(string name);
void DoAction();
void DrawOnScreen();
};
Creature::Creature(string name) {
CreatureName = name;
}
Player::Player(string name){
CreatureName = name;
}
Monster::Monster(string name) {
CreatureName = name;
}
WildPig::WildPig(string name) {
CreatureName = name;
}
Dragon::Dragon(string name) {
CreatureName = name;
}
void Player::DoAction(){
cout<< "Is Attacking!!";
}
void Monster::DoAction(){
cout<< "Is Doing Monster Stuff!!";
}
void WildPig::DoAction(){
cout<< "Is Running!!!";
}
void Dragon::DoAction(){
cout<< "Is Breathing Fire!!";
}
void Player::DrawOnScreen(){
cout<< "Player " <<CreatureName<< " ";
DoAction();
cout<<endl;
}
void Monster::DrawOnScreen(){
cout<< "Monster " <<CreatureName<< " ";
DoAction();
cout<<endl;
}
void WildPig::DrawOnScreen(){
cout<< "WildPig" <<CreatureName<< " ";
DoAction();
cout<<endl;
}
void Dragon::DrawOnScreen(){
cout<< "Dragon " <<CreatureName<< " ";
DoAction();
cout<<endl;
}
int main(){
Player hero("Kick_Ass");
Monster mon("UFO");
WildPig pig("i'm hungry'");
Dragon drag("i'm_the_Boss");
Creature* object[4];
object[0]=&hero;
object[1]=&mon;
object[2]=&pig;
object[3]=&drag;
object[0]->DrawOnScreen();
object[1]->DrawOnScreen();
object[2]->DrawOnScreen();
object[3]->DrawOnScreen();
return 0;
}
Output

You might also like