Name - Manav Jain Uid - 18Bcs1301 Section - Cse 3 B Group - H
Name - Manav Jain Uid - 18Bcs1301 Section - Cse 3 B Group - H
UID - 18BCS1301
SECTION - CSE 3 B
GROUP - H
C++ ASSIGNMENT 2
Static class members refer to that members of a class which are defined using
the ‘static’ keyword. This means that only one instance of static members exist,
no matter how many objects are created. The characteristics of static class
members are as follows:
Eg-
#include<iostream>
using namespace std;
class abc
{
public:
static int count;
abc()
{
count++;
}
};
Int abc::count=0;
void main()
{
abc ob1,ob2,ob3;
Q2 Class teacher-
Private members - Name, Subj, Basic, DA, HRA,
salary
Calculate() calculates salary, ie sum of basic, da,
hra.
Public members- readdata(), displaydata()
#include<iostream>
#include<conio.h>
using namespace std;
class teacher
{
char name[50],subj[20];
float basic,da,hra,sal;
float calculate()
{
da=0.05*basic;
hra=0.1*basic;
sal=basic+da+hra;
return sal;
}
public:
void readdata()
{
cout<<"Enter the name of the teacher\n";
gets(name);
cout<<"Enter the subject of the teacher\n";
gets(subj);
cout<<"Enter the basic salary\n";
cin>>basic;
sal=calculate();
}
void displaydata()
{
cout<<"Name - "<<name<<"\nSubject - "<<subj<<"\nBasic salary -
"<<basic<<endl;
cout<<"DA - "<<da<<"\nHRA - "<<hra<<"\nTotal Salary - "<<sal<<endl;
}
};
void main()
{
teacher ob;
ob.readdata();
ob.display();
}
#include<iostream>
using namespace std;
void main()
{
int x;
cout<<"Enter an integer\n";
cin>>x;
try
{
if(x>=0)
throw 0;
else
throw 2.92;
}
catch(int p)
{
cout<<"The int is non negative\n";
}
catch(float q)
{
cout<<"The int is negative\n";
}
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
friend ostream & operator << (ostream &out, const Complex &c);
friend istream & operator >> (istream &in, Complex &c);
};
int main()
{
Complex c1;
cin >> c1;
cout << "The complex object is ";
cout << c1;
return 0;
}