Вы находитесь на странице: 1из 24

6 Code for Program of swapping numbers by call by reference in C++

Programming
#include<iostream.h>
#include<conio.h>
void swap (int &a, int &b)
{
/* &a and &b are reference variables */
int temp;
temp=a;
a=b;
b=temp;
}
main()
{
clrscr();
int i=5,j=10;
cout<<"Before swapping I = "<<i<<" J = "<<j<<endl;
swap(i,j);
cout<<"After swapping I = "<<i<<" J = "<<j<<endl;
}

7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2

#include<iostream>
using namespace std;
void swap(int,int);
int main()
{
int a,b;
cout<<"Enter Value Of 1st no.:";
cin>>a;
cout<<"Enter Value of 2nd no.:";
cin>>b;
cout<<"Before swapping\nValue of 1st no. is "<<a<<"\nValue of 2nd no. is "<<b;
swap(a,b);
cout<<"\nOutside function after swapping\nValue of 1st no. is "<<a<<"\nValue of 2nd
no. is "<<b;
}
void swap(int a,int b)
{
int c;
c=a;
a=b;
b=c;
cout<<"\nInside function after swapping\nValue of 1st no. is "<<a<<"\nValue of 2nd
no. is "<<b;
}

8 constructor & destructor


#include <iostream.h>
#include<conio.h>
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor

private:
double length;
};

// Member functions definitions including constructor


Line::Line(void)
{
cout << "Object is being created" << endl;
}

void Line::setLength( double len )


{
length = len;
}

double Line::getLength( void )

{
return length;
}
// Main function for the program
int main( )
{
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
getch();

return 0;
}

8.destructor
#include <iostream.h
#include<conio.h>
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration

private:
double length;
};

// Member functions definitions including constructor


Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
}

void Line::setLength( double len )


{
length = len;
}

double Line::getLength( void )


{
return length;
}
// Main function for the program
int main( )
{

Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
getch();
return 0;
}

9. Use of this Pointer. Using class


#include<iostream.h>
#include<conio.h>
class p1
{
private:
char c1[10];
public:
void funct()
{
cout<<"Object Address : "<<this<<"\n";
}
};
void main()
{
clrscr();
p1 w1;
w1.funct();

getch();
}

10. . Programs to Implement Inheritance and Function Overriding. a. Multiple


inheritance Access Specifiers
#include <iostream.h>
#include<conio.h>
// Base class Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

// Base class PaintCost


class PaintCost
{
public:

int getCost(int area)


{
return area * 70;
}
};

// Derived class
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
};

int main(void)
{
Rectangle Rect;
int area;

Rect.setWidth(5);
Rect.setHeight(7);

area = Rect.getArea();

// Print the area of the object.


cout << "Total area: " << Rect.getArea() << endl;

// Print the total cost of painting


cout << "Total paint cost: $" << Rect.getCost(area) << endl;
getch();

return 0;
}

10. Hierarchical inheritance Function Overriding /Virtual Function


#include <iostream>
using namespace std;
class B
{
public:
virtual void display()

/* Virtual function */

{ cout<<"Content of base class.\n"; }


};

class D1 : public B
{
public:
void display()
{ cout<<"Content of first derived class.\n"; }
};

class D2 : public B
{
public:
void display()
{ cout<<"Content of second derived class.\n"; }
};

int main()
{

B *b;
D1 d1;
D2 d2;

/* b->display(); // You cannot use this code here because the function of base class
is virtual. */

b = &d1;
b->display(); /* calls display() of class derived D1 */
b = &d2;
b->display(); /* calls display() of class derived D2 */
return 0;
}

11 unary
#include<iostream.h>
#include<conio.h>

class complex
{
int a,b,c;
public:
complex(){}
void getvalue()
{
cout<<"Enter the Two Numbers:";
cin>>a>>b;
}

void operator++()
{
a=++a;
b=++b;
}

void operator--()
{
a=--a;

b=--b;
}

void display()
{
cout<<a<<"+\t"<<b<<"i"<<endl;
}
};

void main()
{
clrscr();
complex obj;
obj.getvalue();
obj++;
cout<<"Increment Complex Number\n";
obj.display();
obj--;
cout<<"Decrement Complex Number\n";
obj.display();
getch();
}

Binary
#include <iostream.h>
#include<conio.h>
class Clock {
int hours;
int minutes;
public:
Clock() {};
Clock(int h, int m) { hours = h; minutes = m; }
int hr() { return hours; }
int min() { return minutes; }
int set_hr(int h) { hours = h; }
int set_min(int m) { minutes = m; }
};

Clock & operator+(Clock & t, Clock & t2) {


Clock sum;
sum.set_hr(t.hr() + t2.hr() + (t.min()+t2.min())/60);
sum.set_min((t.min()+t2.min()) % 60);
return sum;
}

ostream & operator<<(ostream & os, Clock & t){


os << t.hr() << " hours, " << t.min() << " minutes";
return os;
}

int main() {
Clock a(1, 40);
Clock b(3, 29);
Clock c(2, 19);
cout << a << endl << b << endl << c << endl;
a = a + b + c;
cout << a << endl;
getch();
return 0;
}

Вам также может понравиться