Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

All Programs-1

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

//friend function to add two distance

#include <iostream>
using namespace std;
class Distance{
private:
float feet;
float inch;
//friend Distance add_dist(Distance,Distance);
public:
void set(float,float);
void display();
friend Distance add_dist(Distance,Distance);
};
void Distance::set(float f, float i){
feet = f;
inch = i;
}
void Distance::display(){
cout<<feet<<"\'"<<inch << "\"";
}
Distance add_dist(Distance d1,Distance d2){
Distance temp;
temp.feet = d1.feet + d2.feet;
temp.inch = d1.inch + d2.inch;
while(temp.inch >=12){
temp.feet ++;
temp.inch = temp.inch-12;
}
return temp;
}
int main(){
Distance d1,d2,d3;
d1.set(5,10);
d2.set(2,11);
d3 = add_dist(d1,d2);
d1.display();
cout << "+";
d2.display();
cout <<"=";
d3.display();
return 0;
}
output:
5'10"+2'11"=8'9"

//friend function
//bridging between two classes
//addition of private data members of two classes
#include<iostream>
using namespace std;
class B;
class A{
private:
int a;
public:
void set(int);
friend void add(A,B);
};
class B{
private:
int b;
public:
void set(int);
friend void add(A,B);
};
void A::set(int n){
a = n;
}
void B::set(int n){
b = n;
}
void add(A oa,B ob){
cout << "sum = " << oa.a+ob.b;
}
int main(){
A objA;
B objB;
objA.set(10);
objB.set(20);
add(objA,objB);
return 0;
}
Output:
sum = 30

//friend function
//bridging between two classes
//swapping private data members of two classes
#include<iostream>
using namespace std;
class B;
class A{
private:
int a;
public:
void set(int);
void display();
friend void swap(A &,B &);
};
class B{
private:
int b;
public:
void set(int);
void display();
friend void swap(A &,B &);
};
void A::set(int n){
a = n;
}
void A::display(){
cout << "a = " << a << endl;
}
void B::set(int n){
b = n;
}
void B::display(){
cout << "b = " << b << endl;
}
void swap(A &oa,B &ob){
int temp = oa.a;
oa.a = ob.b;
ob.b = temp;
}
int main(){
A objA;
B objB;
objA.set(10);
objB.set(20);
cout <<"Before swapping:\n";
objA.display();
objB.display();

swap(objA,objB);

cout <<"After swapping:\n";


objA.display();
objB.display();

return 0;
}
output:
Before swapping:
a = 10
b = 20
After swapping:
a = 20
b = 10

//static data and static function


#include<iostream>
using namespace std;
class Part{
private:
int id;
static int count;
public:
void set(){
count++;
id = count;
}
int get_id(){
return id;
}
static int get_count(){
return count;
}
};
int Part::count = 0;//int Part::count;
int main(){
//cout<<"Count = " << Part::get_count()<<endl;
Part p1;
p1.set();
Part p2;
p2.set();
Part p3;
p3.set();
cout << "p1 ID: "<<p1.get_id()<<endl;
cout << "p2 ID: "<<p2.get_id()<<endl;
cout << "p3 ID: "<<p3.get_id()<<endl;
cout << "----------"<<endl;
cout<<"Count: " << p1.get_count()<<endl;
cout<<"Count: " << p2.get_count()<<endl;
cout<<"Count: " << p3.get_count()<<endl;
return 0;
}
Output:
p1 ID: 1
p2 ID: 2
p3 ID: 3
----------
Count: 3
Count: 3
Count: 3

//addition of two dates using constructors


#include<iostream>
using namespace std;

class Date{
private:
int dd;
int mm;
int yr;
public:
Date(){} //defatult
Date(int d,int m,int y){ //parameterized
dd = d;
mm = m;
yr = y;
}
void display(){
cout<<dd<<"/"<<mm<<"/"<<yr;
}
Date AddDate(Date d){
Date temp;
temp.dd = dd + d.dd;
temp.mm = mm + d.mm;
temp.yr = yr + d.yr;
while(temp.dd>=30){
temp.mm++;
temp.dd = temp.dd-30;
}
while(temp.mm>=12){
temp.yr++;
temp.mm = temp.mm-12;
}
return temp;
}
};
int main(){
Date d1(10,5,1),d2(25,6,2),d3;
d3 = d1.AddDate(d2);
d1.display();
cout<<"+";
d2.display();
cout<<"=";
d3.display();
return 0;
}
Output:
10/5/1+25/6/2=5/0/4

//example of constructor and destructor


#include <iostream>
using namespace std;
class Test{
private:
static int count;
public:
Test(){
count++;
cout<<"Constructor is called for object: " << count<<endl;
}
~Test(){ //destructor
cout<<"Object " << count << " is destroyed." << endl;
count--;
}
};
int Test::count = 0;
int main(){
Test t1;
Test t2;
Test t3;
return 0;
}
output:
Constructor is called for object: 1
Constructor is called for object: 2
Constructor is called for object: 3
Object 3 is destroyed.
Object 2 is destroyed.
Object 1 is destroyed.

You might also like