You are on page 1of 13

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

Enter balance: 1000


Account no is: 121
Balance is : 1000

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;
}
};
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();
}
OUTPUT:
Enter account no : 121
6

cout << "n Enter the Input


Value: ";
cin>>x;
cout << "n The Output is: " <<
fun(x);
}
inline int fun(int x1)
{
return 5*x1;
}
OUTPUT:
Enter the Input Value: 5
The Output is : 25

OPERATOR OVERLOADING
#include <iostream>
class temp
{
private:
int count;
public:
temp():count(5)

FRIEND FUNCTION:
1 #include <iostream>
2 class fun
3 {
4 private:
5
int a,b;
6 public:
7
void test()
8
{
9
a=100;
10
b=200;
11
}
12
friend int compute(fun
e1);
13 };
14 int compute(fun e1)
15 {
16
return int(e1.a+e1.b)5;
17 }
18 void main()
19 {
20
fun e;
21
e.test();
22
cout << "The result is:" <<
compute(e);

void operator ++()


{
count=count+1;
}
void Display()
{
cout<<"Count: "<<count; }
};
int main()
{
temp t;
++t;
/* operator
function void operator ++() is called */
t.Display();
return 0;
}
}
Output
Count: 6

INLINE FUNCTION:
#include <iostream>
int fun(int);
void main( )
{
int x;
7

23

}
OUTPUT:
The result is : 295
COMMAND LINE ARGUMENTS:

ANOTHER PROGRAM
FOR FRIEND FUNCTION:
#include<iostream.h>
#include<conio.h>
class base
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return
float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
base obj;
obj.get();
cout<<"\n Mean value is :
"<<mean(obj);
getch();

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char
*argv[]) // command line
arguments
{
if(argc!=5)
{
printf("Arguments passed
through command line " \
"not equal to 5");
return 1;
}
printf("\n Program name :
%s \n", argv[0]);
printf("1st arg : %s \n",
argv[1]);
printf("2nd arg : %s \n",
argv[2]);
printf("3rd arg : %s \n",
argv[3]);
printf("4th arg : %s \n",
argv[4]);
printf("5th arg : %s \n",
argv[5]);
return 0;
}

}
Output:
Enter two values: 10, 20
Mean Value is: 15

Output:
Program name : test
1st arg : this
8

2nd arg : is
3rd arg : a
4th arg : program
5th arg : (null)

ANOTHER PROGRAM FOR VIRTUAL


FUNCTION:

VIRTUAL FUNCTION:

#include <iostream>
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 = &d1;
b->display(); /* calls display() of
class derived D1 */

#include <iostream>
class B
{
public:
void display()
{ cout<<"Content of base
class.\n"; }
};
class D : public B
{
public:
void display()
{ cout<<"Content of derived
class.\n"; }
};
int main()
{
B *b;
D d;
b->display();
b = &d; /* Address of object d in
pointer variable */
b->display();
return 0;
}
Output
Content of base class.
Content of base class.

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

}
OUTPUT:
Error message : Unable to read....

Output

The standard log stream (clog):


#include <iostream>
int main( )
{
char str[] = "Unable to read....";
clog << "Error message : " << str
<< endl;
}
OUTPUT:
Error message : Unable to read....

Content of first derived class.


Content of second derived class.
C++ Basic Input/Output
The standard output stream (cout):
#include <iostream>
int main( )
{
char str[] = "Hello C++";
cout << "Value of str is : " << str
<< endl;
}
OUTPUT:
Value of str is : Hello C++
The standard input stream (cin):
#include <iostream>
int main( )
{
char name[50];
cout << "Please enter your name: ";
cin >> name;
cout << "Your name is: " << name
<< endl;
}
OUTPUT:
Please enter your name: Sana
Your name is: Sana
The standard error stream (cerr):
#include <iostream>
int main( )
{
char str[] = "Unable to read....";
cerr << "Error message : " << str <<
endl;

TYPE CASTING OR TYPE


CONVERSION
Automatic Conversion
otherwise called as Implicit
Conversion:
#include <iostream>
void main()
{
short x=6000;
int y;
y=x;
}
Type casting otherwise called
as Explicit Conversion:
#include <iostream.h>
void main()
{
int a;
float b,c;
cout << "Enter the value of a:";
cin >> a;
cout << "Enter the value of b:";
cin >> b;
c = float(a)+b;
10

clear();
while(infile)
{
----}
and
while(infile.read())
{
---}

cout << "The value of c is:"<<c;


}
OUTPUT:
Enter the value of a:10
Enter the value of b:12.5
The value of c is :22.5

ERROR HANDLING DURING


FILE OPERATION:
----if stream infile;
infile.open(ABC);
while(!infile.fail())
{
----- process the file
------}
if(infile.eof())
{
----- terminate program normally
}
else
if(inline.bad())
{
--- report error
}
else
{
infile clear(); \\ clear error state
}
--------11

INDEX FOR C++ PROGRAMS


S.N
O
1.
2.

3.
4.
5.
6.
7.

8.
9.
10.
11.
12.
13.
14.
15.

TITLE
MANIPULATORS:
ENDL,SETW,SETFILL,SETPRECISION.
INHERITANCE:
SINGLE
MULTIPLE
MULTI LEVEL
PASS BY VALUE
PASS BY REFERENCE
POINTER
POINTER & ARRAY
CONSTRUCTOR & DESTRUCTOR
TYPES OF CONSTRUCTOR:
DEFAULT CONSTRUCTOR
PARAMETERISED CONSTRUCTOR
COPY CONSTRUCTOR
DYNAMIC CONSTRUCTOR
OPERATOR OVERLOADING
INLINE FUNCTION
FRIEND FUNCTION
COMMAND LINE ARGUMENTS
VIRTYAL FUNCTION
C++ BASIC INPUT /OUTPUT
TYPE CASTING OR TYPE CONVERSION
ERROR HANDLING DURING FILE OPERATION

12

PAGE
1
1
2
2
3
3
4
4
4
5
5
5
6
7
7
7
8
9
10
10
11

13

You might also like