Assignments
Unit 2
SF - II
GROUP - A
1) Online application form for
only one user using multilevel inheritance.
2) Working of lift in the form of
infinite loop using multiple inheritance.
3) Washing machine control
(timer=15 min) using hybrid inheritance.
4) Define a class “word” which can
hold a word where the length of the word is specified in the constructor.
Implement the constructor and destructor.
|
5) Create two classes DB and DM which stores the value of distances. DM
stores distance in meter and cm and DB in feet and inches. WAP that can read
values for the class object and add one object of DM with another object of
DB. Use a friend function to carry out the addition operation. The object
that stores the result maybe DM object or DB object depending upon the units
in which result is required.
|
6) Write a c++ program that will
give the conditions of the environment required, food habits and unique
characteristic of pet animal’s fish and dog. Define a base called pet that
describe any common house hold pet; two derived classes called fish and dog
with item specific to that type of animals. Write pure virtual function in
the base class for the operations that are common to both type of animals.
Write a program to test the usage of classes.
Solutions:
1)
#include<iostream>
#include<string.h>
#include<cstdio>
using
namespace std;
class
roll
{
public:
long rollNo;
};
class
name:public roll
{
public:
char name[20],fname[20],mname[20];
};
class
marks:public name
{
public:
int marks[5];
};
class
address:public marks
{
public:
char houseNo[10],village[20],district[20],state[20];
void get()
{
cout<<"Enter your name\t";
gets(name);
cout<<"Enter your father name\t";
gets(fname);
cout<<"Enter your mother's name\t";
gets(mname);
cout<<"Enter your Address\nHouse no.\t";
gets(houseNo);
cout<<"Village/Town\t";
gets(village);
cout<<"District\t";
gets(district);
cout<<"State\t";
gets(state);
cout<<"Enter your Higher Secondary roll number\t";
cin>>rollNo;
cout<<"Enter your marks :\n";
for(int i=0;i<5;i++)
{
cout<<"Marks in subject "<<(i+1)<<"\t";
cin>>marks[i];
}
}
void show()
{
cout<<"\nRoll number :\t"<<rollNo;
cout<<"\nName :\t"<<name;
cout<<"\nFather's name :\t"<<fname;
cout<<"\nMother's name :\t"<<mname;
cout<<"\nAddress\n";
cout<<"House No. : "<<houseNo;
cout<<"\t\tVillage/Town : "<<village;
cout<<"\nDistrict : "<<district;
cout<<"\t\tState : "<<state;
cout<<"\nMarks are";
for(int i=0;i<5;i++)
cout<<"\nSub "<<(i+1)<<" :
"<<marks[i];
}
};
int
main()
{
address obj;
obj.get();
cout<<"\n\nThe entered details are\n";
obj.show();
return 0;
}
2)
#include<iostream>
#include<windows.h>
#include<stdlib.h>
using
namespace std;
class
ground
{
public:
void show0()
{
cout<<"\nGround level reached";
}
};
class
first
{
public:
void show1()
{
cout<<"\nFirst level reached";
}
};
class
second
{
public:
void show2()
{
cout<<"Second level reached";
}
};
class
third
{
public:
void show3()
{
cout<<"Third level reached";
}
};
class
fourth
{
public:
void show4()
{
cout<<"Fourth level reached";
}
};
class
lift:ground,first,second,third,fourth
{
int floorNo;
public:
void travel()
{
while(1)
{
cout<<"\nEnter floor number\t";
cin>>floorNo;
switch(floorNo)
{
case 0:
Sleep(1000);
show0();
break;
case 1:
for(int i=1;i>=0;i--)
{
cout<<"Wait "<<(i)<<" Seconds";
Sleep(1000);
system("cls");
}
show1();
break;
case 2:
for(int i=2;i>=0;i--)
{
cout<<"Wait "<<(i)<<" Seconds";
Sleep(1000);
system("cls");
}
show2();
break;
case 3:
for(int i=3;i>=0;i--)
{
cout<<"Wait "<<(i)<<" Seconds";
Sleep(1000);
system("cls");
}
show3();
break;
case 4:
for(int i=4;i>=0;i--)
{
cout<<"Wait "<<(i)<<" Seconds";
Sleep(1000);
system("cls");
}
show4();
break;
default:
cout<<"\nThis floor is not available";
}
}
}
};
int
main()
{
lift obj;
obj.travel();
return 0;
}
3)
#include<iostream>
#include<stdlib.h>
#include<windows.h>
using
namespace std;
class
clothName
{
public:
char name[20];
void getClothName()
{
cout<<"Enter the cloth name\t";
cin>>name;
}
};
class
stir:public
clothName
//single inheritance
{
public:
void stirring()
{
int i,j;
for(i=8;i>=0;i--)
{
for(j=60;j>=0;j--)
{
cout<<"Stirring \nTime Left\n";
cout<<i<<" : "<<j;
Sleep(1000);
system("cls");
}
}
}
};
class
rinse :public
stir
//multilevel inheritance
{
public:
void rinseing()
{
int i,j;
for(i=7;i>=0;i--)
{
for(j=60;j>=0;j--)
{
cout<<"Rinsing \nTime Left\n";
cout<<i<<" : "<<j;
Sleep(1000);
system("cls");
}
}
}
};
int
main()
{
rinse obj;
obj.getClothName();
obj.stirring();
obj.rinseing();
cout<<"Cloth Washed";
return 0;
}
|
4)
#include<iostream>
using
namespace std;
class
word
{
char *name;
public:
word(int n)
{
name = new char[n];
cout<<"Word of length "<<n<<" is created";
}
~word()
{
delete name;
cout<<"\nThe word is deleted";
}
};
int
main()
{
word obj(10);
return 0;
}
5)
#include<iostream>
using
namespace std;
class DB;
class DM
{
float distm;
float distcm;
public:
void getDM()
{
cout<<"\nEnter the distance in meter and centimeter\t";
cin>>distm>>distcm;
}
friend void sum(DM,DB);
};
class DB
{
float distft;
float distinch;
public:
void getDB()
{
cout<<"\nEnter the distance in feet and inches\t";
cin>>distft>>distinch;
}
friend void sum(DM,DB);
};
void
sum(DM obj1,DB obj2)
{
int choice;
cout<<"\nPress 1 for answer in meters and centimeters\t";
cout<<"\nPress 2 for result in Feet and inch";
cout<<"\nEnter your choice\t";
cin>>choice;
switch(choice)
{
case 1:
obj1.distcm=obj1.distcm+obj2.distft*12*2.54 + obj2.distinch*2.54;
if(obj1.distcm>=100)
{
obj1.distm = obj1.distm+(int)(obj1.distcm/100);
obj1.distcm=obj1.distcm- (int)(obj1.distcm/100)*100;
}
cout<<"\nthe dist is \t"<<obj1.distm<<"
m "<<obj1.distcm<<" cm";
break;
case 2:
obj2.distinch= obj2.distinch + obj1.distm*100/2.54 + obj1.distcm/2.54;
if(obj2.distinch>=12)
{
obj2.distft+=(int)(obj2.distinch/12);
obj2.distinch=obj2.distinch- (int)(obj2.distinch/12)*12;
}
cout<<"\nThe Distance calculated is
\t"<<obj2.distft<<" ft
"<<obj2.distinch<<" inch";
break;
default:
cout<<"\nEntered wrong choice";
}
}
int main ()
{
cout<<"UID 16BCS7102";
DM obj1;
DB obj2;
obj1.getDM();
obj2.getDB();
sum(obj1,obj2);
return 0;
}
6)
#include<iostream>
#include<string.h>
using
namespace std;
class pet
{
public:
char petname[20];
pet()
{
}
pet(char *name)
{
strcpy(petname,name);
}
virtual void environment()=0;
};
class
dog:public pet
{
public:
dog(char *name)
{
strcpy(petname,name);
}
void environment()
{
cout<<"\nHi I am "<<petname<<"and i like to live
in my house";
}
};
class
fish:public pet
{
public:
strcpy(petname,name);
}
void environment()
{
cout<<"\nHi I am "<<petname<<" and i like to swim
i water";
}
};
int
main()
{
pet *base1,*base2;
dog dog("Dog");
fish fish("Fish");
base1=&dog;
base2->environment();
return 0;
}
No comments:
Post a Comment