0% found this document useful (0 votes)
10 views

C++ by SHIVAM

C++ for project bca

Uploaded by

rarya2256
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C++ by SHIVAM

C++ for project bca

Uploaded by

rarya2256
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

PRACTICAL OF C++

1. Write a program to read two integers and display them.


Use cin and cout statements.

#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 :-

2.Write a program to use different formats of typecasting and


display the converted values.

#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++

cout<<"\n int in char format : "<<(char)a;


cout<<"\n float in int format : "<<(int)f;
cout<<"\n double in char format : "<<(char)d;
cout<<"\n char in int format : "<<(int)c;
}

Output :-

3. Write a program to calculate the length of string. Use run-


time declaration and initialization of variables.

#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++

4. Write a program to demonstrate implicit type conversion.

#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 :-

5. Write a program to enter two characters and display the


larger character. Use nested if-else statements.

#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 :-

6. Write a program using do-while loop to print numbers and


their cubes up to 10.

#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 :-

7. Write a program to demonstrate return by reference.

#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 :-

8. Write a program to calculate square of an integer and float


number. Define function sqr() . Use function overloading concept.

#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 :-

9. Write a program to declare private member function and access it


using public member function.

#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 :-

10. Write a program to declare static public member variable , global


and local variable with the same name . Initialize and display their
contents.

#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 :-

11. Write a program to pass an object with reference to constructor.


Declare and initialize other objects.

#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++

13. Write a program to perform addition of two objects using


operator keyword.

#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 :-

14. Write a program to declare protected data in base class. Access


data of base class declared under private section using member
function of derived class.

#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

You might also like