C++ by SHIVAM
C++ by SHIVAM
#include<iostream.h>
#include<conio.h>
void main()
{
int num1,num2;
cout<<"Enter Two numbers : ";
cin>>num1>>num2;
cout<<"Entered Numbers are : ";
cout<<num1<<"\t"<<num2;
}
Output :-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=66;
float f=2.5;
double d=85.22;
char c='K';
1|P a g e
PRACTICAL OF C++
Output :-
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char name[15];
cout<<"\n Enter your Name : ";
cin>>name;
int len=strlen(name);
cout<<"\n The length of the string is : "<<len;
}
Output :-
2|P a g e
PRACTICAL OF C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int j=2.54;
int k='A';
char c=87.5;
cout<<"\n j = "<<j;
cout<<"\n k = "<<k;
cout<<"\n c = "<<c;
}
Output :-
#include<iostream.h>
#include<constream.h>
void main()
{
3|P a g e
PRACTICAL OF C++
clrscr();
char j , k;
cout<<"\nEnter a character : ";
j=getche();
cout<<"\nEnter another character : ";
k=getche();
cout<<endl;
if(j>k)
{
cout<<j<<" is larger than "<<k;
}
else if(k>j)
{
cout<<k<<" is larger than "<<j;
}
else
{
cout<<" Both the characters are same.";
}
}
Output :-
#include<iostream.h>
#include<constream.h>
#include<math.h>
4|P a g e
PRACTICAL OF C++
void main()
{
clrscr();
int y , x=1;
cout<<"\nNumbers and their cubes :-"<<endl;
do
{
y=pow(x,3);
cout<<x<<"\t\t"<<y<<endl;
x++;
}while(x<=10);
}
Output :-
#include<iostream.h>
5|P a g e
PRACTICAL OF C++
#include<constream.h>
int &large(int &p , int &q);
void main()
{
clrscr();
int l , k;
cout<<"\nEnter values of l and k : ";
cin>>l>>k;
large(l,k)=120;
cout<<"l = "<<l<<" k = "<<k<<endl;
}
int &large(int &p,int &q)
{
if(p>q) return p;
else return q;
}
Output :-
#include<iostream.h>
#include<constream.h>
int sqr(int);
float sqr(float);
void main()
{
6|P a g e
PRACTICAL OF C++
int a=15;
float b=2.5;
cout<<"\n Square of a = "<<sqr(a);
cout<<"\n Square of b = "<<sqr(b);
}
int sqr(int s)
{
return(s*s);
}
float sqr(float j)
{
return(j*j);
}
Output :-
#include<iostream.h>
#include<constream.h>
class item
{
private:
int codeno;
float price;
int qty;
public:
void values()
{
codeno=125;
price=195;
7|P a g e
PRACTICAL OF C++
qty=200;
}
void show()
{
cout<<"\n Codeno = "<<codeno;
cout<<"\n Price = "<<price;
cout<<"\n Quantity = "<<qty;
}
};
void main()
{
item one;
one.values();
one.show();
}
Output :-
#include<iostream.h>
#include<constream.h>
int c=11;
class bita
{
public:
static int c;
};
int bita::c=22;
8|P a g e
PRACTICAL OF C++
void main()
{
clrscr();
int c=33;
cout<<"\nClass member c = "<<bita::c;
cout<<"\nGlobal variable c = "<<::c;
cout<<"\nLocal variable c = "<<c;
}
Output :-
#include<iostream.h>
#include<constream.h>
class num
{
int n;
public:
num()
{
n=0;
}
num(int k)
{
n=k;
}
num(num &j)
{
n=j.n;
}
void show()
9|P a g e
PRACTICAL OF C++
{
cout<<n;
}
};
void main()
{
clrscr();
num J(50);
num K(J);
num L=J;
num M;
M=J;
cout<<"\n Object J value of n : ";
J.show();
cout<<"\n Object K value of n : ";
K.show();
cout<<"\n Object L value of n : ";
L.show();
cout<<"\n Object M value of n : ";
M.show();
}
Output :-
12. Write a program to declare classes within classes and access the
variable of both the classes.
#include<iostream.h>
10 | P a g e
PRACTICAL OF C++
#include<constream.h>
class A
{
public:
int x;
A ()
{
x=10;
}
class B
{
public:
int y;
B()
{
y=20;
}
void show()
{
A a;
cout<<"\nx = "<<a.x;
cout<<"\ny = "<<y;
}
};
};
void main()
{
clrscr();
A::B b;
b.show();
}
Output :-
11 | P a g e
PRACTICAL OF C++
#include<iostream.h>
#include<constream.h>
class number
{
public:
int x;
int y;
number()
{
x=0;
y=0;
}
number(int j , int k)
{
x=j;
y=k;
}
number operator + (number d)
{
number t;
t.x=x+d.x;
t.y=y+d.y;
return t;
}
void show()
{
cout<<"\nx = "<<x<<" y = "<<y;
}
};
void main()
{
clrscr();
number a(2,3),b(4,5),c;
a.show();
12 | P a g e
PRACTICAL OF C++
b.show();
cout<<"\nAddition of two objects (a & b) :";
c=a+b;
c.show();
}
Output :-
#include<iostream.h>
#include<constream.h>
class A
{
protected:
int x;
};
class B : private A
{
int y;
public:
B()
{
x=30;
y=40;
}
13 | P a g e
PRACTICAL OF C++
void show()
{
cout<<"\n x = "<<x;
cout<<"\n y = "<<y;
}
};
void main()
{
clrscr();
B b;
b.show();
}
Output :-
15. Write a program to create derived class from the qualifier class.
#include<iostream.h>
#include<constream.h>
class A
{
public:
int x;
A() {}
class B
{
public:
int y;
B() {}
};
};
14 | P a g e
PRACTICAL OF C++
class C:public A , A :: B
{
int z;
public:
C(int j, int k, int l)
{
x=j;
y=k;
z=l;
}
void show()
{
cout<<"\n x = "<<x;
cout<<"\n y = "<<y;
cout<<"\n z = "<<z;
}
};
void main()
{
clrscr();
C c(10,20,30);
c.show();
}
Output :-
15 | P a g e