0% found this document useful (0 votes)
25 views41 pages

C++ Edit 5.1

Uploaded by

muzammil844641
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views41 pages

C++ Edit 5.1

Uploaded by

muzammil844641
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

01 Use Manipulators (setw ,endl)

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main ()
{
clrscr();
cout<<setw(10)<<"English"<<setw(5)<<9<<endl;
cout<<setw(10)<<"Computer"<<setw(5)<<98<<
endl;
cout<<setw(10)<<"Maths"<<setw(5)<<70<<endl
;
cout<<setw(10)<<"Physics"<<setw(5)<<50<<end
l;
getch();
return 0;
}

shoeb
OUTPUT
02  Reference variable

#include<iostream.h>
#include<conio.h>
int main ()
{
int i=10;
int &a=i;
clrscr();
cout<<"Value of i is "<<i<<endl;
cout<<"value of i refference is "<<a;
getch();
return 0;
}
OUTPUT
03->Simple ‘ if ‘ Statment

#include<iostream.h>
#include<conio.h>
int main ()
{
int a;
clrscr();
cout<<"enter your age ";
cin>> a;
if (a>18)
{
cout<<"\n you can vote";
}
getch ();
return 0;
}
OUTPUT
04 if else Statment
#include<iostream.h>
#include<conio.h>
int main ()
{
int a;
clrscr();
cout<<"Enter your age ";
cin>>a;
if (a>18)
{
cout<<"\n you can vote ";
}
else
{
cout<<"\n sorry you can't vote";
}
getch();
return 0;
}
OUTPUT
05Nesting of if-else
#include<iostream.h>
#include<conio.h>
int main ()
{
int a,b;
clrscr();
cout<<"Enter the value of a"<<endl;
cin>>a;
cout<<"Enter the value of b"<<endl;
cin>>b;
if (a!=b) {
cout<<"\n a is not equal to b";
if (a>b) {
cout<<"\n a is greater than b";
}
else {
cout<<"\n a is less than b";
}
}
else {
cout<<"\n a is equal to b";
}
getch();
return 0;
}

OUTPUT
06else if ladder
#include<iostream.h>
#include<conio.h>
int main ()
{
int a;
clrscr();
cout<<"Enter your age ";
cin>>a;
if (a>=18) {
cout<<"Your age is "<<a<<"\n You can vote";
}
else if (a>10) {
cout<<"Your age is "<<a<<"\n You can vote only
for below the age 18 people ";
}
else if (a>6) {
cout<<"Your age is "<<a<<"\n You can vote for
small kids";
}
else {
cout<<"you can't vote \n because your age is very
below";
}
getch();
return 0;
}

OUTPUT
07 for loop
#include<iostream.h>
#include<conio.h>
int main ()
{
clrscr();
for (int a=1;a<=5;a++)
{
cout<<"\n"<<a;
}
getch();
return 0;
}
OUTPUT
08 Do-while loop
#include<iostream.h>
#include<conio.h>
int main ()
{
int i=1;
clrscr();
do {
cout <<"\n"<<i;
i++;
}
while(i<=8);
getch ();
return 0;
}
OUTPUT
09 while loop
#include<iostream.h>
#include<conio.h>
int main ()
{
int i=1;
clrscr();
while (i<=10)
{
cout<<i<<"\n";
i=i++;
}
getch();
return 0;
}
OUTPUT
10 Basic data type and operator :
#include <iostream.h>
#include <conio.h>
int main () {
//integer data type
int num1=10;
int num2=5;
//addition
int sum=num1+num2;
cout<<"sum:"<<sum<<endl;
//sustraction
int difference=num1+num2;
cout<<"difference:"<<difference<<endl;
//maltipication
int product=num1*num2;
cout<<"product:"<<product<<endl;
//devision
int quotient=num1/num2;
cout<<"quotient:"<<quotient<<endl;
//floating-point data type
double pi=3.14159;
double radius=2.0;
//calculate a area of a circle
double area=pi*radius*radius;
cout<<"area of a circle:"<<area<<endl;
//character data type
char grade='A';
cout<<"grade:"<<grade<<endl;
getch();
}

OUTPUT
11 Using inline function
#include<iostream.h>
#include<conio.h>
inline int cube(int s)
{
return s*s*s;
}
int main()
{
clrscr();
cout<<"the cube of 3
is:"<<cube(3)<<endl;
getch();
}
OUTPUT
12concept of friend function
#include<iostream.h>
#include<conio.h>
class marks;
class student
{
public:
int rno;
char *name;
void getdata(int r,char *n)
{
rno=r;
name=n;
}
friend void getresult(student s,marks m);
}s;
class marks
{
public:
int s1,s2,s3;
void getmarks(int m1,int m2,int m3)
{
s1=m1;
s2=m2;
s3=m3;
}
friend void getresult(student s,marks m)
}m;
void getresult(student s,marks m)
{
int total,avg;
total=m.s1+m.s2+m.s3;
avg=total/3;
cout<<"roll no:"<<s.rno<<endl;
cout<<"name:"<<s.name<<endl;
cout<<"total marks :"<<total<<endl;
cout<<"avg marks :"<<avg<<endl;
if(m.s1>=35 && m.s2>=35 && m.s3>=35)
cout<<"result:passed";
else
cout<<"Result:failed";
}
void main()
{
clrscr();
s.getdata(20,"Shoeb");
m.getmarks(90,90,90);
getresult(s,m);
getch();

OUTPUT
13Addition Using Function Overloading
#include<iostream.h>
#include<conio.h>
add(int x)
{
int z=x+x;
cout<<"square of "<<x<<" is:
"<<z<<endl;
cout<<"------------------\n";
}
add (int a,int b)
{
cout<<"addition is : "<<a+b<<endl;
cout<<"---------------\n";
}
add(int a,int b,int c)
{
c=a+b;
cout<<a<<"+"<<b<<"= "<< c <<endl;
cout<<"------------\n";
}
void main()
{
clrscr();
add(2);
add(20,30);
add(100,200,300);
getch();
}
OUTPUT
14Implement A Class
#include<iostream.h>
#include<conio.h>
class number {
private:
double value;
public:
number(double val):value(val){}
number operator+(const number& other){
return number(value+other.value); }
number operator-(const number& other){
return number (value-other.value); }
number operator*(const number& other) {
return number (value*other.value); }
number operator/(const number& other) {
if (other.value !=0) {
return number(value /other.value); }
else {
cerr<<"error:division by zero!"<<endl;
return number(0.0); } }
void display(){
cout<<"value : "<<value<<endl; } };
int main() {
clrscr();
number num1(10.0);
number num2(5.0);
number diff=num1-num2;
number sum =num1+num2;
number product =num1*num2;
number quotient =num1/num2;
cout<<"num1: ";
num1.display();
cout<<"num2: ";
num2.display();
cout<<"sum: ";
sum.display();
cout<<"difference: ";
diff.display();
cout<<"product: ";
product.display();
cout<<"quotient: ";
quotient.display();
getch();
}
OUTPUT
15Demonstrate The Use Of Virtual Function
#include<iostream.h>
#include<conio.h>
class base {
public:
virtual void print()
{
cout<<"print the class"<<endl;
}
void show()
{
cout<<"show base class"<<endl;
}
};
class derived:public base {
public:
void print()
{
cout<<"print derived class"<<endl;
}
void show()
{
cout<<"show derived class"<<endl;
}
};
int main()
{
clrscr();
base*bptr;
derived d;
bptr=&d;
bptr->print();
bptr->show();
getch();
}

OUTPUT
16 Demonstrate Use Of Single Inheritance
#include<iostream.h>
#include<conio.h>
class base {
public:
int x;
void getdata()
{
cout<<"enter the value of x = ";cin>>x;
}
};
class derive:public base
{
private:
int y;
public:
void readdata()
{
cout<<"enter the value of y = ";cin>>y;
}
void product()
{
cout<<"product = "<<x*y;
}
};
int main() {
clrscr();
derive a;
a.getdata();
a.readdata();
a.product();
getch();
return 0;
}
OUTPUT
17Demonstrate Use Of Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
class animal {
public:
void eat() {
cout<<"eating..."<<endl;
}
};
class dog:public animal {
public:
void bark() {
cout<<"barking...";
}
};
int main(void) {
clrscr();
dog dl;
dl.eat();
dl.bark();
getch();
return 0;
}
OUTPUT
18Demonstrate Use Of Class
#include<iostream.h>
#include<conio.h>
class person {
public:
char name[30];
int number;
};
int main()
{
clrscr();
person obj;
cout<<"enter the name : ";
cin>>obj.name;
cout<<"enter the number : ";
cin>>obj.number;
cout<<obj.name<<" : "<<obj.number;
getch();
}
OUTPUT
19Unary Operator Overloading
#include<iostream.h>
#include<conio.h>
class distance{
public:
int feet,inch;
distance(int f,int i) {
this ->feet=f;
this ->inch=i;
}
void operator+()
{
feet++;
inch++;
cout<<"\n feet and
inches(increament): "<<feet<<"
"<<inch;
}
};
int main() {
clrscr();
distance dl(8,9);
+dl;
getch();
return 0;
}
OUTPUT
20Binary Operator Overloading
#include<iostream.h>
#include<conio.h>
class Box {
double length;
double breadth;
double height;
public:
double getvolume(void) {
return length*breadth*height;
}
void setlength(double len) {
length=len;
}
void setbreadth(double bre) {
breadth=bre;
}
void setheight(double hei) {
height=hei;
}
Box operator+(const Box& b) {
Box box;
box.length=this->length+b.length;
box.breadth=this->breadth+b.breadth;
box.height=this->height+b.height;
return box;
}
};
int main() {
clrscr();
Box Box1;
Box Box2;
Box Box3;
double volume=0.0;

Box1.setlength(6.0);
Box1.setbreadth(7.0);
Box1.setheight(5.0);

Box2.setlength(12.0);
Box2.setbreadth(13.0);
Box2.setheight(10.0);

volume=Box1.getvolume();
cout<<"\n volume of Box1 : "<<volume<<endl;
volume=Box2.getvolume();
cout<<"volume of Box2 : "<<volume<<endl;
Box3=Box1+Box2;
volume=Box3.getvolume();
cout<<"volume of Box3 : "<<volume<<endl;
getch();
return 0;
}
OUTPUT

You might also like