0% found this document useful (0 votes)
47 views

Name - Manav Jain Uid - 18Bcs1301 Section - Cse 3 B Group - H

Static class members refer to members defined with the static keyword that are shared among all objects of a class. They have the following characteristics: 1) All objects share the static member 2) A change to a static member by one object is reflected in all objects 3) Static members are initialized to 0 if not explicitly initialized 4) Static members must be declared outside the class

Uploaded by

Ayush
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Name - Manav Jain Uid - 18Bcs1301 Section - Cse 3 B Group - H

Static class members refer to members defined with the static keyword that are shared among all objects of a class. They have the following characteristics: 1) All objects share the static member 2) A change to a static member by one object is reflected in all objects 3) Static members are initialized to 0 if not explicitly initialized 4) Static members must be declared outside the class

Uploaded by

Ayush
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME - MANAV JAIN

UID - 18BCS1301
SECTION - CSE 3 B
GROUP - H

C++ ASSIGNMENT 2

Q1. What do you mean by static class members?


Explain the characteristics of static members with
suitable examples

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:

1. All objects of a class share the static data member.


2. If one object makes a change to a static variable, it is reflected in all the
objects.
3. All static members are initialized to 0 if they are not initialized in the
beginning.
4. We have to declare the static data members outside of the class using the
scope resolution operator.

Eg-
#include<iostream>
using namespace std;

class abc
{
public:
static int count;

abc()
{
count++;
}
};

Int abc::count=0;

void main()
{
abc ob1,ob2,ob3;

cout<<”No of objects created are - “<<count;


}

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();
}

Q3. WAP that shows how certain exception types


are not allowed to be thrown.

#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";
}

Q4. Class Y has been derived from class X. Class Y


does not contain any data members. Does Y require
a constructor?
If class Y is derived from class X and it doesn’t have any data members, then
there is a need to create a constructor of class Y. This is because if there is a
non default constructor of base class, then derived class constructor must call
the base class constructor using the super keyword.

Q5. How to overload global >> and << operators to


work with cin,cout,cerretc?

#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);
};

ostream & operator << (ostream &out, const Complex &c)


{
out << c.real;
out << "+i" << c.imag << endl;
return out;
}

istream & operator >> (istream &in, Complex &c)


{
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imagenory Part ";
in >> c.imag;
return in;
}

int main()
{
Complex c1;
cin >> c1;
cout << "The complex object is ";
cout << c1;
return 0;
}

You might also like