Question: 1
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> using namespace std; class student { private: int roll; char name[20]; float per; public: student(); //default constructor void read(); void show(); }; //default constructor student::student() { roll=0; name[0]='\0'; per=0; } 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; s1.show(); s2.show(); return 0; }
Output:
roll 0 name per 0
roll 0 name per 0
Question:2
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> using namespace std; class employee { private: int empno; char name[20]; float sal; public: employee(); //default constructor void read(); void show(); }; //default constructor employee::employee() { empno=0; name[0]='\0'; sal=0; } 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; s1.show(); s2.show(); return 0; }
Output:
empno 0 name sal 0
empno 0 name sal 0
Question: 3
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> using namespace std; class bank { private: int ano; char name[20]; float bal; public: bank(); //default constructor void read(); void show(); }; //default constructor bank::bank() { ano=0; name[0]='\0'; bal=0; } 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; s1.show(); s2.show(); return 0; }
Output:
ano 0 name bal 0
ano 0 name bal 0