Assignments Unit-2
SF - II
GROUP
- B
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 hierarchy inheritance.
4) Write
a c++ program to swap data using function template.
|
5) What
is function template? Write the syntax for the function template. Write an
example program for function template.
6) Write
a program to print the following output constructor and destructor.
Output:
My profession is: student
My age is : 20
I can walk. I can talk. I can teach
engineering.
My profession is: footballer
My age is : 19
I can walk. I can talk. I can play football.
|
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<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;
template<typename T>
void swapNo(T &a,T &b)
{
T temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int
a,b;
cout<<"Enter
two numbers\t";
cin>>a>>b;
swapNo(a,b);
cout<<"The
swapped values are\t"<<a<<" "<<b;
return
0;
}
5)
Template is basically a generic class
which can be used for any datatype.
In C++ template is defined by the
following syntax.
template<class template_ name> or
template<typename template_name>
function template is the using template
for particular function. For example the bellow short program demonstrates the
use of function template in c++.
#include<iostream>
using namespace std;
template<typename T>
T add(T a,T b)
{
return
a+b;
}
int main()
{
int
a,b;
cout<<"Enter
two numbers\t";
cin>>a>>b;
cout<<"The
sum of two numbers is \t"<<add(a,b);
return
0;
}
Here the above function template adds two numbers of
integer type , we can give any type of datatype inside function as parameter
but both parameter should be of same type.
6)
#include<iostream>
using namespace std;
class intro
{
public:
intro()
{
cout<<"My
profession is : Student";
cout<<"\nMy
age is 20.";
cout<<"\nI
can walk. I can talk. I can teach Engineering";
}
~intro()
{
cout<<"\nMy
profession is : Footballer";
cout<<"\nMy
age is 19.";
}
};
int main()
{
intro
obj;
return
0;
SAMPLE OUTPUT:

No comments:
Post a Comment