Sei sulla pagina 1di 6

MANIPULATORS:

Endl Manipulator:
#include<stdio.h>
#include<conio.h>
void main()
{
cout<<HELLO<<endl;
cout<<BCA<<endl;
}
Output:
HELLO
BCA
Setw Manipulators:
#include<stdio.h>
#include<iomanip.h>
void main()
{
int x1= 1,x2=2,x3=3;
cout<<setw(8)<<Hello<<setw(20)
<<bca<<endl;
<<setw(8)<<Read<<setw(20)<<x
1<<endl;
<<setw(8)<<Well<<setw(20)<<x2
<<endl;
<<
setw(8)<<Girls<<setw(20)<<x3<<
endl;
}
OUTPUT:
Hello bca
Read 1
Well 2
Girls 3
Setfill manipulators:
#include<stdio.h>
#include<iomanip.h>
void main()
{
cout<<setw(10)<<setfill(@)<<5<
<3<<endl;
}
OUTPUT:
@@@@@@@@53
Setprecision manipulators:
#include<stdio.h>
#include<iomanip.h>
void main()
{
float x=0.1;

cout<<fixed<<setprecision(3)<<x<<
endl;
cout<<scientific<<setprecision(15)<
<x<<endl;
}
OUTPUT:
0.100
0.100E-001
INHERITANCE:
SINGLE INHERITANCE:
class base
{
char name[20];
int rollno;cChar gender;
Public:
void getdata();
void display();
};
class derived:public base
{
float height,weight;
public:
void getfit();
void displayfit();
};
void base::getdata()
{
cout<<Enter the name of a
student:<<endl;
cin>>name;
cout<<Enter the roll no of a
student:<<endl;
cin>>rollno;
cout<<Enter the gender of a
student:<<endl;
cin>>gender;
}
void base::display()
{
cout<<Name=<<name;
cout<<Rollno=<<rollno;
cout<<Gender=<<gender;
}
void derived::getfit()
{
cout<<Enter the height & weight of a
student:;
1

cin>>height>>weight;
}
void derived ::displayfit()
{
cout<<Height=<<height;
cout<<weight=<<weight;
}
int main()
{
derived d;
d.getdata();
d.display();
d.getfit();
d.displayfit();
return ();
}
OUTPUT:
Enter the name of a student: Sana
Enter the roll no of a student: 101
Enter the gender of a student: female
Name = Sana
Roll no=101
Gender= female
Enter the height & weight of a
student:5.6 42
Height = 5.6
Weight = 42

}
void n:: get_n ( int y)
{
n=y;
}

MULTIPLE INHERITANCE:

class student
{
protected :
int rollno;
public:void getno();
void putno();
};
class test:public student
{
protected:
int m1,m2,m3,m4,m5,m6;
public:
void getmarks();
void putmarks();
};
class result:public test
{
int total;
public:
void display();
};
void student::getno()

void p::display()
{
cout<<M=<<m;
cout<<N=<<n;
cout<<M*N=<<m * n;
}
int main()
{
p s;
s.get_m(10);
s.get_n(20);
s.display();
getch();
return o;
}
OUTPUT:
M = 10
N =20
M* N = 200
MULTI LEVEL INHERITANCE:

class m
protected:
int m;
public:void get_m(int x);
};
class n
{
protected :
int n;
public:
void get_n(int y);
};
class p:public m,public n
{
public:
void display();
};
void m::get_m ( int x)
{
m = x;
2

{
cout<<Enter the roll no of a
student:;
cin>>rollno;
}
void student :: putno()
{
cout<<Rollno=<<rollno;
}
void test :: getmarks()
{
cout<<Enter six subject marks one
by one:;
cin>>m1>>m2>>m3>>m4>>m5>>
m6;
}
void test ::putmarks()
{
cout<<Marks
are:<<m1<<m2<<m3<<m4<<m5
<<m6;
}
void result::display()
{
Total=m1+m2+m3+m4+m5+m6;
cout<<Total =<<total;
}
int main()
{
result s1;
s1.getno();
s1.getmarks();
s1.putno();
s1.putmarks();
s1.display();
return 0 ;
}
OUTPUT:
Enter the roll no of a student: 101
Enter six subject marks one by one: 80
87
89
90
90
67
Rollno= 101
Marks are : : 80
87
89
90

90
67
Total = 503

PASS BY VALUE:
#include<iostream.h>
void swap( int , int);
void main()
{
int var1,var2;
cout<<Enter two numbers:<<endl;
cin>>var1>>var2;
cout<<In
Main :<<var1<<var2<<endl;
swap(var1,var2);
}
void swap ( int num1,int num2)
{
int temp;
temp=num1;
num1=num2;
num2=temp;
cout<<In
Swap:<<num1<<num2<<endl;
}
OUTPUT:
Enter two numbers: 2 4
In Main: 2
4
In Swap: 4 2
PASS BY REFERENCE:
#include<iostream.h>
void main()
{
int number=5;
int & ref= number;
cout<<Number
is :<<number<<endl;
cout<<Increasing the number
.<<endl;
number++;
cout<<Now number is : <<number
<<endl;
ref++;

cout<<Now reference
is :<<ref<<endl;
cout<<Now number
is :<<number<<endl;
}
OUTPUT:
Number is : 5
Increasing the number .
Now number is : 6
Now reference is : 7
Now number is : 7

P++;
}
p=age;
cout<<sum of
ages :<<sum<<endl;
cout<<The age of the last student
is :<<*(p+4)<<endl;
return 0;
}
OUTPUT:
Enter the age of a student: 12
Enter the age of a student: 11
Enter the age of a student: 15
Enter the age of a student: 17
Enter the age of a student: 18
sum of ages: 73
The age of the last student is : 18

POINTER:
#include<iostream.h>
int main()
{
int * x;
int c=200;
int p;
x=&c;
p=*x;
cout<<The Address of memory
location of x:<<x<<endl;
cout<<The content of the pointer
x:<<*x<<endl;
cout<<The content of the variable p:
<<p <<endl;
return 0;
}
OUTPUT:
The Address of memory location of x:
0012f
The content of the pointer x: 200
The content of the variable p: 200

CONSTRUCTOR & DESTRUCTOR:


#include<iostream.h>
class simple
{
private:
int a,b;
public:
simple()
{
a=0;
b=0;
cout<<\n Constructor of class
simple;
}
~ simple()
{
cout<<\n Destructor of class simple;
}
void getdata()
{
cout<<\n Enter values for a & b:;
cin>>a>>b;
}
void putdata()
{
cout<<\n The two
integers :<<a<<b;
cout<<\n The sum of the
variables :;<<a+b;
}
};

POINTERS & ARRAYS:


#include<iostream.h>
int main()
{
int age[5];
int * p;
int sum=0,i;
p = age;
for(i=0;i<5;i++)
{
cout<<Enter the age of a
student:<<endl;
cin>>*p;
sum=sum+*p;
4

void main()
{
simple s;
s.getdata();
s.putdata();
}
OUTPUT:
Constructor of class simple
Enter values for a & b: 2 4
The two integers : 2 4
The sum of the variables :6
Destructor of class simple

class integer
{
int m,n;
public:
integer ( int x, int y);
void display();
};
integer :: integer (int x, int y)
{
m=x;
n=y;
}
void integer ::display()
{
cout<<M value is = <<m<<endl;
cout<<N value is = <<n<<endl;
}
int main()
{
integer pc(20,50);
cout<<Demonstration of
parameterized constructor\n;
pc.display();
return 0;
}
OUTPUT:
Demonstration of parameterized
constructor
M value is = 20
N value is = 50

DEFAULT CONSTRUCTOR:
#include<iostream.h>
class integer
{
Private:
int m,n;
public:
integer ();
void display();
};
integer :: integer()
{
m=0;
n= 0;
}
void integer :: display()
{
cout<<M value is = <<m <<endl;
cout<<N value is = <<n <<endl;
}
int main()
{
cout<<Demonstration of Default
constructor\n;
integer dc;
dc.display();
return 0;
}
OUTPUT:
Demonstration of Default constructor
M value is = 0
N value is = 0

COPY CONSTRUCTOR:
#include<iostream.h>
class example
{
int a,b;
public:
example ( int x, int y)
{
a=x;
b=y;
cout<<\n Im constructor;
}
void display()
{
cout<<\n Values :<<a<<\t<<b;
}
};
int main()
{

PARAMETERISED CONSTRUCTOR:
#include<iostream.h>
5

example object1(10,20);
example object2=object1;
object1.display();
object2.display();
return 0;
}
OUTPUT:
Im constructor
Values : 10 20
Values : 10 20

}
};
void main()
{
clrscr();
int an,bal;
cout<< "Enter account no : ";
cin >> an;
cout<< "\nEnter balance : ";
cin >> bal;
Account *acc=new
Account(an,bal); //dynamic constructor
acc->display();
//'->' operator is used
to access the method
getch();
}

DYNAMIC CONSTRUCTOR:
#include <iostream.h>
#include <conio.h>
class Account
{
private:
int account_no;
int balance;
public :
Account(int a,int b)
{
account_no=a;
balance=b;
}
void display()
{
cout<< "\nAccount number is : "<<
account_no;
cout<< "\nBalance is : " << balance;

OUTPUT:
Enter account no : 121
Enter balance: 1000
Account no is: 121
Balance is : 1000

Potrebbero piacerti anche