Question: 1
Write a C++ program to declare a class named student with attributes as roll,name and per, further take input for details and display them?
Sol:
#include <iostream>
#include<string.h>
using namespace std;
class student
{
private:
int roll;
char name[20];
float per;
public:
student(); //default constructor
student(int r,char *n,float p); //Parameterized constructor
void read();
void show();
};
//default constructor
student::student()
{
roll=0;
name[0]='\0';
per=0;
}
//Parameterized constructor
student::student(int r,char *n,float p)
{
roll=r;
strcpy(name,n);//strcpy() : header file : string.h
per=p;
}
void student::read()
{
cout<<"Enter roll,name and per ";
cin>>roll>>name>>per;
}
void student::show()
{
cout<<"roll "<<roll<<" name "<<name<<" per "<<per<<endl;
}
int main()
{
student s1,s2(1001,"Kapil",45000);
s1.show();
s2.show();
return 0;
}
Output:
roll 0 name per 0
roll 1001 name Kapil per 45000
Question : 2
Write a C++ program to declare a class named employee with attributes as empno, name and salary, further take input for details and display them?
Sol:
#include <iostream>
#include<string.h>
using namespace std;
class employee
{
private:
int empno;
char name[20];
float sal;
public:
employee(); //default constructor
employee(int a,char *n,float s); //Parameterized constructor
void read();
void show();
};
//default constructor
employee::employee()
{
empno=0;
name[0]='\0';
sal=0;
}
//Parameterized constructor
employee::employee(int a,char *n,float b)
{
empno=a;
strcpy(name,n);//strcpy() : header file : string.h
sal=b;
}
void employee::read()
{
cout<<"Enter empno,name and sal ";
cin>>empno>>name>>sal;
}
void employee::show()
{
cout<<"empno "<<empno<<" name "<<name<<" sal "<<sal<<endl;
}
int main()
{
employee s1,s2(1001,"Kapil",45000);
s1.show();
s2.show();
return 0;
}
Output:
empno 0 name sal 0
empno 1001 name Kapil sal 45000
Question: 3
Write a C++ program to declare a class named bank with attributes as ano, name and bal, further take input for details and display them?
Sol:
#include <iostream>
#include<string.h>
using namespace std;
class bank
{
private:
int ano;
char name[20];
float bal;
public:
bank(); //default constructor
bank(int a,char *n,float s); //Parameterized constructor
void read();
void show();
};
//default constructor
bank::bank()
{
ano=0;
name[0]='\0';
bal=0;
}
//Parameterized constructor
bank::bank(int a,char *n,float b)
{
ano=a;
strcpy(name,n);//strcpy() : header file : string.h
bal=b;
}
void bank::read()
{
cout<<"Enter ano,name and bal ";
cin>>ano>>name>>bal;
}
void bank::show()
{
cout<<"ano "<<ano<<" name "<<name<<" bal "<<bal<<endl;
}
int main()
{
bank s1,s2(1001,"Sumit",56000);
s1.show();
s2.show();
return 0;
}
Output:
ano 0 name bal 0
ano 1001 name Sumit bal 56000




