Академический Документы
Профессиональный Документы
Культура Документы
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;
}
private:
double length;
};
{
return length;
}
// Main function for the program
int main( )
{
Line line;
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;
};
Line line;
getch();
}
// 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();
return 0;
}
/* Virtual function */
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; }
};
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;
}