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

Past Paper Practice

The document contains C++ code defining classes for shapes (Shape, Rectangle, Triangle) and overloading operators. The main function creates shape objects (rectangles and triangles), gets their dimensions from user, calculates their areas using the showarea virtual function, and writes the object data to a binary file. It handles the objects polymorphically using pointer to base Shape class. The classes define protected length and width variables to store shape dimensions and pure virtual showarea function to calculate area in derived classes.

Uploaded by

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

Past Paper Practice

The document contains C++ code defining classes for shapes (Shape, Rectangle, Triangle) and overloading operators. The main function creates shape objects (rectangles and triangles), gets their dimensions from user, calculates their areas using the showarea virtual function, and writes the object data to a binary file. It handles the objects polymorphically using pointer to base Shape class. The classes define protected length and width variables to store shape dimensions and pure virtual showarea function to calculate area in derived classes.

Uploaded by

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

2019 Qno.

2
#include <iostream>
#include <string.h>
using namespace std;
class Course
{
private:
int cnum;
char cname[100];
public:
Course(): cnum(0)
{
strcpy(cname,"");
}
Course(int num,char name[]): cnum(num)
{
strcpy(cname,name);
}
void show()
{
cout<<"Course Number is : "<<cnum<<endl;
cout<<"Course Name is : "<<cname<<endl;
}
};
class Labcourse: public Course
{
private:
int labhrs;
public:
Labcourse(): Course(), labhrs(0)
{

}
Labcourse(int num,char name[],int lh):Course(num,name),labhrs(lh)
{

}
void show()
{
Course::show();
cout<<"Lab Hours : "<<labhrs<<endl;
if(labhrs==4)
{
cout<<"This is Labcourse "<<endl;
}
else
{
cout<<"This is Not a Labcourse "<<endl;
}
}
};
int main()
{
Labcourse l1(101,"OOP",3);
l1.show();
return 0;
}

2019 Qno.3
#include <iostream>
#include <string.h>
using namespace std;
class Shape
{
private:
int X,Y;
char name[100];
public:
Shape():X(0),Y(0)
{
strcpy(name,"");
}
void get()
{
cout<<"Enter X-coordinate : ";
cin>>X;
cout<<"Enter Y-coordinate : ";
cin>>Y;
cout<<"Enter Shape Name : ";
cin.ignore();
cin.getline(name,100);
}
void show() const
{
cout<<"X-coordinate : "<<X<<endl;
cout<<"Y-coordinate : "<<Y<<endl;
cout<<"Shape Name : "<<name<<endl;
}
bool operator ==(Shape ss)
{
if(X==ss.X && Y==ss.Y && strcmp(name,ss.name)==0)
return true;
else
return false;
}
};
int main()
{
Shape s1,s2,s3;
cout<<"\t\t~~Enter Shape-1 Details~~"<<endl;
s1.get();
cout<<"\t\t~~Enter Shape-2 Details~~"<<endl;
s2.get();
if((s1==s2)==true)
cout<<"\nBoth Shapes are Equal " <<endl;
else
cout<<"\nBoth Shapes are Not Equal "<<endl;
return 0;
}

2019 Qno.4
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Publication
{
private:
char title[100];
float price;
public:
Publication():price(0.0f)
{
strcpy(title,"");
}
virtual void get()
{
cout<<"Enter Price : ";
cin>>price;
cout<<"Enter Title : ";
cin.ignore();
cin.getline(title,100);
}
virtual void show()
{
cout<<"Price : "<<price<<endl;
cout<<"Title : "<<title<<endl;
}
virtual bool IsOverSize()=0;
virtual void IsOverSize2()=0;
};
class Book: public Publication
{
private:
int page;
public:
Book():Publication(),page(0)
{

}
void get()
{
Publication::get();
cout<<"Enter Pages : ";
cin>>page;
}
void show()
{
Publication::show();
cout<<"Pages : "<<page<<endl;
}
bool IsOverSize()
{
if(page>500)
return true;
else
return false;
}
void IsOverSize2()
{
cout<<"OverSIZE Book "<<endl;
}
};

class Tape: public Publication


{
private:
float playtime;
public:
Tape():Publication(),playtime(0.0f)
{

}
void get()
{
Publication::get();
cout<<"Enter Playtime(in Minutes) : ";
cin>>playtime;
}
void show()
{
Publication::show();
cout<<"Playtime : "<<playtime<<endl;
}
bool IsOverSize()
{
if(playtime>90)
{
return true;
}
else
return false;
}
void IsOverSize2()
{
cout<<"OverSIZE TAPE "<<endl;
}
};
int main()
{
Publication *ptr[10];
int choice;
int i=0;
char opt;
do
{
cout<<"1. For Book "<<endl;
cout<<"2. For Tape "<<endl;
cout<<"\nEnter Your Choice : ";
cin>>choice;
if(choice==1)
{
ptr[i]= new Book();
cout<<"\n\t\t~~Details FOr BOOK~~"<<endl;
ptr[i]->get();
cout<<"\n\t\t~~Details You Entered~~"<<endl;
ptr[i]->show();
if(ptr[i]->IsOverSize()==true)
{
ptr[i]->IsOverSize2();
}
}
else if(choice==2)
{
ptr[i]= new Tape();
cout<<"\n\t\t~~Details FOr TAPE~~"<<endl;
ptr[i]->get();
cout<<"\n\t\t~~Details You Entered~~"<<endl;
ptr[i]->show();
if(ptr[i]->IsOverSize()==true)
{
ptr[i]->IsOverSize2();
}
}
else
{
cout<<"Invalid Choice ...Try Again "<<endl;
exit(1);
}
i++;
cout<<"\nDo You want To Continue (y/n) : ";
cin>>opt;
}
while(opt=='y' || opt=='Y');
return 0;
}
2019 Qno.5
#include <iostream>
#include <exception>
#include <stdlib.h>

using namespace std;


class Distance
{
private:
int feets;
float inches;
public:
Distance():feets(0),inches(0.0f)
{}
Distance(int f,float i):feets(f),inches(i)
{}
friend istream & operator>> (istream&,Distance &);
friend ostream & operator<< (ostream&,Distance &);
~Distance()
{}
};
istream & operator>> (istream& in,Distance & dd)
{
cout<<"Enter Feets :";
in>>dd.feets;
cout<<"Enter Inches :";
in>>dd.inches;
if(dd.feets<0 || dd.inches<0.0f )
{
throw runtime_error("\nDistance must be Positive\n YOu should Try Again");
}
while(dd.inches>=12)
{
dd.feets++;
dd.inches=dd.inches-12;
}
}
ostream & operator<< (ostream& out,Distance & dd)
{
out<<"\n\t ~~Distance is~~:\n";
out<<"=====================================\n\t\t"<<endl;
out<<"\t\t"<<dd.feets<<"\'"<<dd.inches<<"\''"<<endl;
out<<"====================================="<<endl;
}
int main()
{
Distance d1;
try
{
cin>>d1;
}
catch(runtime_error &r)
{
cout<<"\toh no..."<<r.what()<<endl;
exit(1);
}
cout<<d1;

return 0;
}

2019 Qno.6
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include<string.h>
using namespace std;
class Text
{
public:
void upper()
{
ifstream file;
file.open("C:\Bs.txt",ios::in);
if(!file)
{
cout<<"FIle Not found "<<endl;
exit(1);
}
char ch,ch1;
while(!file.eof())
{
file.get(ch);
ch1=toupper(ch);
cout<<ch1;
}
file.close();
}
void countt()
{
int count=0;
ifstream file;
file.open("C:\Bs.txt",ios::in);
if(!file)
{
cout<<"FIle Not found "<<endl;
exit(1);
}
string ch;

while(!file.eof())
{
file>>ch;
if(ch=="is")
{
count++;
}
}
cout<<"\n\nTotal Words Named as *is* are "<<count<<endl;
}
};
int main()
{
Text t1;
t1.upper();
t1.countt();
return 0;
}

2022 Qno.1
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string.h>

using namespace std;


class Shape
{
protected:
double length ,width;
public:
Shape():length(0.0f),width(0.0f)
{

}
virtual void get()
{
cout<<"Enter Length : ";
cin>>length;
cout<<"Enter Width : ";
cin>>width;
}
virtual double showarea()=0;
};
class Rectangle : public Shape
{
private:
double area;
public:
Rectangle():Shape()
{}

double showarea()
{
area=(2*length)*(2*width);
return area;
}
};
class Triangle: public Shape
{
private:
double area;
public:
Triangle():Shape()
{}

double showarea()
{
area=(length*width)/2.0f;
return area;
}
};
int main()
{
Shape *ptr[2];
int opt,i=0;
double area;
char choice;
ofstream fri8;
fri8.open("C:\Computer.bin",ios::binary);
if(!fri8)
{
cout<<"ERrOr...Not Found "<<endl;
}

do
{
cout<<"1. For TRIANGLE "<<endl;
cout<<"2. For Rectangle "<<endl;
cout<<"\nEnter option : ";
cin>>opt;
switch (opt)
{
case 1 :
{
ptr[i]=new Triangle();
cout<<"\n\t\t~Enter Details For Traingle~"<<endl;
ptr[i]->get();
cout<<"\nAREA OF TRIANGLE IS : "<<ptr[i]->showarea();
break;
}
case 2:
{
ptr[i]=new Rectangle();
cout<<"\n\t\t~Enter Details For Rectangle~"<<endl;
ptr[i]->get();

cout<<"\nAREA OF Rectangle IS : "<<ptr[i]->showarea()<<endl;


break;
}
default:
cout<<"Invalid Option...Try Again "<<endl;
}
i++;
cout<<"\n\nDo YOu Want TO COntinue (y/n) : ";
cin>>choice;
}
while(choice=='Y' || choice=='y');

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


{
fri8.write((char*)&ptr[k],sizeof(ptr[k]));
}
for(int j =0; j<2; j++)
{
delete ptr[j];
ptr[j]=NULL;
}
fri8.close();

return 0;
}

2022 Qno.2
#include <iostream>
#include <string.h>
#include <exception>
using namespace std;

class String
{
private:
char str[50];

public:
String()
{
strcpy(str, "");
}

String(char st[])
{
strcpy(str, st);
}
// Overloaded input stream operator
friend istream& operator >> (istream &, String&);

// Overloaded output stream operator


friend ostream& operator << (ostream &, String&);

// Overloaded + operator for string concatenation


friend String operator + (const String &, const String&);
};

istream& operator >> (istream &in, String &s1)


{
cout << "Enter String: ";
in.getline(s1.str, 50);
if (strlen(s1.str) == 0)
{
throw runtime_error("This is not a Valid String");
}
return in;
}

ostream& operator << (ostream &out, String &s1)


{
out << "String(s1+s2) is: " << s1.str << endl;
return out;
}

String operator + (const String &s1, const String &s2)


{
String temp;
strcpy(temp.str, s1.str);
strcat(temp.str, s2.str);
return temp;
}

int main()
{
String s1;
try
{
cin >> s1;
}
catch (runtime_error &r)
{
cout << "Error: " << r.what() << endl;
}

String s2;
s2 = "Imran " + s1;

cout << "===============================" << endl;


cout << " (+)Operator Overloading " << endl;
cout << "===============================" << endl;
cout << s2;
}

You might also like