Name: Maria Sarfraz
Shaikh
PRN: 21510102
Assignment - 3
Q1.
CODE:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
class fruit{
public:
int ct;
};
class apple:public fruit{
public:
apple(int a){
ct=a;
}
};
class Mangoes:public fruit{
public:
Mangoes(int a){
ct=a;
}
};
int main(){
int a,b;
cout<<"Enter the number of apples & mangoes\n";
cin>>a>>b;
apple obj1(a);
Mangoes obj2(b);
cout<<"No of apples = "<<[Link]<<endl;
cout<<"No of mangoes = "<<[Link]<<endl;
cout<<"Total no of fruits = "<<[Link]+[Link]<<endl;
return 0;
}
Q2.
CODE:
#include <bits/stdc++.h>
using namespace std;
class shape{
public:
void out(){
cout<<"This is a Shape"<<endl;
}
};
class polygon: public shape{
public:
void out(){
cout<<"Polygon is a Shape"<<endl;
}
};
class rectangle: public polygon{
public:
void out(){
cout<<"Rectangle is a Polygon"<<endl;
}
};
class triangle: public polygon{
public:
void out(){
cout<<"Triangle is a Polygon"<<endl;
}
};
class square: public rectangle{
public:
void out(){
cout<<"Square is a Rectangle"<<endl;
}
};
int main(){
shape s1;
polygon s2;
rectangle s3;
triangle s4;
square s5;
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
}
Q3.
CODE:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
class staff{
public:
int code;
string name;
void assign(string s, int n){
code=n;
name=s;
}
void print(){
cout<<"Name : "<<name<<endl;
cout<<"code : "<<code<<endl;
}
};
class teacher: public staff{
public:
string subject_publication;
void assign(string a, string b, int n){
name=a;
subject_publication=b;
code=n;
}
void print(){
staff::print();
cout<<"Subject :
"<<subject_publication<<endl;
}
};
class typist: public staff{
public:
int speed;
void assign(string a, int n, int m){
name=a;
code=n;
speed=m;
}
void print(){
staff::print();
cout<<"Speed : "<<speed<<endl;
}
};
class officer: public staff{
public:
int grade;
void assign(string a, int n, int m){
name=a;
grade=m;
code=n;
}
void print(){
staff::print();
cout<<"Grade : "<<grade<<endl;
}
};
class regular: public typist{
public:
void assign(string a, int n, int m){
name=a;
code=n;
speed=m;
}
void print(){
typist::print();
}
};
class casual: public typist{
public:
int payment;
void assign(string a, int n, int m, int o){
name=a;
code=n;
speed=m;
payment=o;
}
void print(){
typist::print();
cout<<"Payment : "<<payment<<endl;
}
};
int main(){
casual obj;
[Link]("maria", 101, 123,923567);
[Link]();
regular obj1;
[Link]("Maria",123,432);
[Link]();
return 0;
}
Q4.
CODE:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
class staff{
public:
int code;
string name;
void assign(string s, int n){
code=n;
name=s;
}
void print(){
cout<<"Name : "<<name<<endl;
cout<<"code : "<<code<<endl;
}
};
class education{
public:
string general,qualification;
void print(){
cout<<"general education :
"<<general<<endl;
cout<<"highest professional qualification :
"<<qualification<<endl;
}
};
class teacher: public staff, public education{
public:
string subject_publication;
void assign(string a, string b, int n, string p,
string q){
name=a;
subject_publication=b;
code=n;
general=p;
qualification=q;
}
void print(){
staff::print();
cout<<"Subject :
"<<subject_publication<<endl;
education::print();
}
};
class typist: public staff{
public:
int speed;
void assign(string a, int n, int m){
name=a;
code=n;
speed=m;
}
void print(){
staff::print();
cout<<"Speed : "<<speed<<endl;
}
};
class officer: public staff, public education{
public:
int grade;
void assign(string a, int n, int m, string p, string
q){
name=a;
grade=m;
code=n;
general=p;
qualification=q;
}
void print(){
staff::print();
cout<<"Grade : "<<grade<<endl;
education::print();
}
};
class regular: public typist{
public:
void assign(string a, int n, int m){
name=a;
code=n;
speed=m;
}
void print(){
typist::print();
}
};
class casual: public typist{
public:
int payment;
void assign(string a, int n, int m, int o){
name=a;
code=n;
speed=m;
payment=o;
}
void print(){
typist::print();
cout<<"Payment : "<<payment<<endl;
}
};
int main(){
casual obj;
[Link]("maria", 101, 123,923567);
[Link]();
cout<<endl;
regular obj1;
[Link]("maria",123,432);
[Link]();
cout<<endl;
officer me;
[Link]("maria", 01, 24546567, "[Link]",
"NDA Qualified");
[Link]();
return 0;
}
Q5.
CODE:
#include <bits/stdc++.h>
using namespace std;
class Plants{
public:
Plants(){
cout<< "This is a producer\n";
}
};
class Herbivores: public Plants{
public:
Herbivores(){
cout<< "This is a primary consumer\n";
}
};
class Carnivore: public Herbivores{
public:
Carnivore(){
cout<< "This is a secondary consumer\n";
}
};
class Secondary_Carnivore: public Carnivore{
public:
Secondary_Carnivore(){
cout<< "This is a tertiary consumer\n";
}
};
int main() {
Secondary_Carnivore s;
return 0;
}
Q6.
CODE:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
class POLAR{
float r;
float theta;
public:
POLAR(){
r=0;
theta=0;
}
POLAR(float a, float b){
r=a;
theta=b;
}
POLAR operator +(POLAR m){
float ax,ay;
float bx,by;
ax=r*cosf32(theta*3.141592/180);
ay=r*sinf32(theta*3.141592/180);
bx=m.r*cosf32([Link]*3.141592/180);
by=m.r*sinf32([Link]*3.141592/180);
float ansx,ansy;
ansx=ax+bx;
ansy=ay+by;
cout<<ansx<<" "<<ansy<<endl;
r=sqrt(ansx*ansx+ansy*ansy);
float p=ansy/ansx;
cout<<p<<endl;
theta=atan(p);
POLAR pass(r,theta);
return pass;
}
void print(){
cout<<"r = "<<r<<" angle =
"<<theta*180/3.141592<<endl;
}
};
int main(){
float a,b;
cin>>a>>b;
POLAR p1(a,b);
cin>>a>>b;
POLAR p2(a,b);
POLAR p;
p=p1+p2;
[Link]();
return 0;
}
Q7.
CODE:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
class fruit{
public:
int ct;
};
class apple:public fruit{
public:
apple(int a){
ct=a;
}
};
class Mangoes:public fruit{
public:
Mangoes(int a){
ct=a;
}
};
int main(){
int a,b;
cout<<"Enter the number of apples &
mangoes\n";
cin>>a>>b;
apple obj1(a);
Mangoes obj2(b);
cout<<"No of apples = "<<[Link]<<endl;
cout<<"No of mangoes = "<<[Link]<<endl;
cout<<"Total no of fruits =
"<<[Link]+[Link]<<endl;
return 0;
}
Q8.
CODE:
#include<bits/stdc++.h>
using namespace std;
class MAT{
int a[2][2];
public:
void accept(){
cout<<"\n\n Enter 4 element : ";
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
cin>>a[i][j];
}
}
void display(){
for(int i=0;i<2;i++){
cout<<endl;
for(int j=0;j<2;j++)
cout<<" "<<a[i][j];
}
}
MAT operator+(MAT M2){
MAT M3;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
M3.a[i][j]=a[i][j]+M2.a[i][j];
}
}
return M3;
}
MAT operator*(MAT M2){
MAT M3;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
M3.a[i][j]=0;
for(int k=0;k<2;k++){
M3.a[i][j]=(a[i][k]*M2.a[k][j])+M3.a[i][j];
}
}
}
return M3;
}
};
int main()
{
MAT M1,M2,M3;
cout<<"Enter Matrix M1 value: ";
[Link]();
cout<<endl;
cout<<"Enter Matrix M2 value: ";
[Link]();
cout<<endl;
M3=M1+M2;
cout<<"Addition of M1+M2 : ";
[Link]();
cout<<endl;
M3=M1*M2;
cout<<"Multiplication of M1*M2 : ";
[Link]();
cout<<endl;
return 0;
}
Q9.
CODE:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
class String{
char str[20];
public:
void assign(){
scanf("%s", &str);
}
int operator ==(String s)
{
if(!strcmp(str,[Link]))
return 1;
return 0;
}
};
int main()
{
String s1,s2;
cout<<"Enter first string : ";
[Link]();
cout<<"\nEnter second string : ";
[Link]();
if(s1==s2)
{
cout<<"\nStrigs are Equal\n";
}
else
{
cout<<"\nStrings are Not Equal\n";
}
return 0;
}