0% found this document useful (0 votes)
15 views10 pages

Program 10, 11, 12

The document discusses implementation of static member functions and static data members in C++ classes. It defines a class 'test' with a static data member 'count' to count the number of objects. Static member function 'showcount()' is used to print the count and non-static function 'setcode()' increments the count and assigns a unique code to each object. The output shows the count incrementing with each object creation and a unique code assigned to each object.

Uploaded by

mk.kanna10hockey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Program 10, 11, 12

The document discusses implementation of static member functions and static data members in C++ classes. It defines a class 'test' with a static data member 'count' to count the number of objects. Static member function 'showcount()' is used to print the count and non-static function 'setcode()' increments the count and assigns a unique code to each object. The output shows the count incrementing with each object creation and a unique code assigned to each object.

Uploaded by

mk.kanna10hockey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

//Implementation of Destructors//

#include<iostream>
using namespace std;
int count=0;
class test{
public:
test(){
count++;
cout<<"\nConstructor Msg:Object number "<<count<<" created..";
}
~test(){
cout<<"\nDestructor Msg:Object number "<<count<<" destroyed..";
count--;
}
};
int main(){
cout<<"Inside the main block..";
cout<<"\nCreating first object T1..";
test T1;
{
cout<<"\nInside Block 1..";
cout<<"\nCreating two more objects T2 and T3..";
test T2, T3;
cout<<"\nLeaving Block 1...";
}
cout<<"\nBack inside the main block..";
return 0;
}

Output:

Inside the main block..


Creating first object T1..
Constructor Msg:Object number 1 created..
Inside Block 1..
Creating two more objects T2 and T3..
Constructor Msg:Object number 2 created..
Constructor Msg:Object number 3 created..
Leaving Block 1...
Destructor Msg:Object number 3 destroyed..
Destructor Msg:Object number 2 destroyed..
Back inside the main block..
Destructor Msg:Object number 1 destroyed..
//Overloading using Binary Operators//
#include<iostream>
#include<conio.h>
using namespace std;
class complex{
float x, y;
public:
complex(){}
complex(float real, float imag){
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c){
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void){
cout<<x<<" +j"<<y<<"\n";
}
int main(){
complex C1, C2, C3;
C1=complex(2.5, 3.5);
C2=complex(1.6, 2.7);
C3=C1+C2;
cout<<"C1+";
C1.display();
cout<<"C2=";
C2.display();
cout<<"Sum C3=";
C3.display();
getch();
return 0;
}

Output:

C1+2.5 +j3.5
C2=1.6 +j2.7
Sum C3=4.1 +j6.2
//Overloading using Unary Operators//
#include<iostream>
using namespace std;
class space{
int x, y, z;
public:
void getdata(int a,int b,int c);
void display(void);
void operator-();
};
void space::getdata(int a, int b, int c){
x=a;
y=b;
z=c;
}
void space::display(void){
cout<<"x="<<x<<" ";
cout<<"y="<<y<<" ";
cout<<"z="<<z<<"\n";
}
void space::operator-(){
x=-x;
y=-y;
z=-z;
}
int main(){
space S;
S.getdata(10, -20, 30);
cout<<"S: ";
S.display();
-S;
cout<<"-S: ";
S.display();
return 0;
}

Output:

S: x=10 y=-20 z=30


-S: x=-10 y=20 z=-30

(Output for Multilevel)

Output:

Roll Number:111
Marks in Subject 1=75
Marks in Subject 2=59.5
Total=134.5
//Multilevel Inheritance//
#include<iostream>
using namespace std;
class student{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student::get_number(int a){
roll_number=a;
}
void student::put_number(){
cout<<"Roll Number:"<<roll_number<<"\n";
}
class test:public student{
protected:
float sub1, sub2;
public:
void get_marks(float, float);
void put_marks(void);
};
void test::get_marks(float x, float y){
sub1=x;
sub2=y;
}
void test::put_marks(){
cout<<"Marks in Subject 1="<<sub1<<"\n";
cout<<"Marks in Subject 2="<<sub2<<"\n";
}
class result:public test{
float total;
public:
void display(void);
};
void result::display(void){
total=sub1+sub2;
put_number();
put_marks();
cout<<"Total="<<total<<"\n";
}
int main(){
result student1;
student1.get_number(111);
student1.get_marks(75.0, 59.5);
student1.display();
return 0;
}

//Counting Objects in a File//


#include<iostream>
#include<fstream>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
class emp{
char name[30];
int ecode;
public:
emp(){}
emp(char *n, int c){
strcpy(name, n);
ecode=c;
}
};
int main(){
emp e[4];
e[0]=emp("Amit",1);
e[1]=emp("Joy",2);
e[2]=emp("Rahul",3);
e[3]=emp("Vikas",4);
fstream file;
file.open("Employee.dat", ios::in | ios::out);
int i;
for(i=0; i<4; i++)
file.write((char *) &e[i],sizeof(e[i]));
file.seekg(0, ios::end);
int end=file.tellg();
cout<<"Number of objects stored in Employee.dat is "<<i<<endl;
sizeof(emp);
return 0;
}

Output:

Number of objects stored in Employee.dat is 4

//Arrays of objects//
#include<iostream>
#include<conio.h>
using namespace std;
class employee{
char name[10];
float age;
public:
void getdata(void);
void putdata(void);
};
void employee::getdata(void){
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter age: ";
cin>>age;
}
void employee::putdata(void){
cout<<"Name:"<<name<<”\n”;
cout<<"Age:"<<age<<”\n”;
}
const int size=3;
int main(){
employee manager[size];
for(int i=0;i<size;i++){
cout<<"\nDetails of the Manager:"<<i+1<<"\n";
manager[i].getdata();
}
cout<<"\n";
for(int j=0;j<size;j++){
cout<<"\nDetails of the Manager:"<<j+1<<"\n";
manager[j].putdata();
}
getch();
return 0;
}

(Given below-Output for static member function)

Output:

Count:2
Count:3
Object Number:1
Object Number:2
Object Number:3

(Given below-Output of arrays)

Output:

Details of the Manager:1


Enter Name: Krishna
Enter age: 25

Details of the Manager:2


Enter Name: Yamuna
Enter age: 36

Details of the Manager:3


Enter Name: Katherine
Enter age: 30

Details of the Manager:1


Name:Krishna
Age:25

Details of the Manager:2


Name:Yamuna
Age:36

Details of the Manager:3


Name:Katherine
Age:30

(Given below-Output of multiple constructors)

Output:

A=2.7+j3.5
B=1.6+j1.6
C=4.3+j5.1

P=2.5+j3.9
Q=1.6+j2.5
R=4.1+j6.4

//To illustrate the implementation of static member //


#include<iostream>
#include<conio.h>
using namespace std;
class item{
int number;
static int count;
public:
void getdata(int a){
number=a;
count++;
}
void getcount(void){
cout<<"Count:"<<count<<"\n";
}
};
int item::count;
int main(){
item a, b, c;
a.getcount();
b.getcount();
c.getcount();

a.getdata(100);
b.getdata(200);
c.getdata(300);

cout<<"After reading data"<<"\n";


a.getcount();
b.getcount();
c.getcount();

getch();
return 0;
}

(Given below-Output of static datat members)

Output:

Count:0
Count:0
Count:0
After reading data
Count:3
Count:3
Count:3
//To implement the multiple constructors or overloaded constructors//
#include<iostream>
#include<conio.h>
using namespace std;
class complex{
float x, y;
public:
complex(){}
complex(float a){
x=y=a;
}
complex(float real, float imag){
x=real;
y=imag;
}
friend complex sum(complex, complex);
friend void show(complex);
};
complex sum(complex c1, complex c2){
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
}
void show(complex c){
cout<<c.x<<"+j"<<c.y<"\n";
}
int main(){
complex A(2.7, 3.5);
complex B(1.6);
complex C;
C=sum(A, B);
cout<<"A=";show(A);
cout<<"\nB=";show(B);
cout<<"\nC=";show(C);
complex P, Q, R;
P=complex(2.5, 3.9);
Q=complex(1.6, 2.5);
R=sum(P, Q);
cout<<"\n";
cout<<"\nP=";show(P);
cout<<"\nQ=";show(Q);
cout<<"\nR=";show(R);
return 0;
}
//To illustrate implementation of static member function//
#include<iostream>
#include<conio.h>
using namespace std;
class test{
int code;
static int count;
public:
void setcode(void){
code = ++count;
}
void showcode(void){
cout<<"Object number:"<<code<<"\n";
}
static void showcount(void){
cout<<"count:"<<count<<"\n";
}
};
int test::count;
int main(){
test t1, t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
return 0;
}

You might also like