C++ reduced list 2021
C++ reduced list 2021
#include<iostream.h>
int main()
{
int *p,n,i;
cout<<"Enter the limit";
cin>>n;
p=new int[n];
cout<<"Enter integer values";
for(i=0;i<n;i++)
{
cin>>p[i];
}
cout<<"\nEntered elements are";
for(i=0;i<n;i++)
{
cout<<p[i]<<endl;
}
delete []p;
return 0;
}
2. Write a C++ program to implement inline function
#include<iostream.h>
inline void square()
{
int n,s;
cout<<"\n Enter a number";
cin>>n;
s=n*n;
cout<<"\n Square is "<<s;
}
int main()
{
square();
return 0;
}
3. Write a C++ program to find the area using function
overloading.
#include<iostream.h>
class shape
{
public:
void area(int s)
{
int a;
a=s*s;
cout<<"\n Area of square is "<<a;
}
void area(double r)
{
double a;
a=3.14*r*r;
cout<<"\n Area of circle is "<<a;
}
};
int main()
{
shape ob;
int s,l,b;
double r;
ob.area(s);
ob.area(r);
ob.area(l,b);
return 0;
}
4. Write a C++ program to swap the integer data using friend
function.
#include<iostream.h>
class Demo
int x,y;
public:
void read()
cin>>x>>y;
void print()
cout<<"\n x= "<<x;
cout<<”\n y= “<<y;
int t;
t=ob.x;
ob.x=ob.y;
ob.y=t;
int main()
Demo ob;
ob read();
ob.print();
swap(ob);
ob.print();
return 0;
};
#include<iostream.h>
class Test
{
int x,y;
public:
Test(int a, int b)
{
x=a;
y=b;
}
Test(Test &ob)
{
x=ob.x;
y=ob.y;
}
void show()
{
cout<<"\n x= "<<x;
cout<<"\n y= "<<y;
}
};
int main()
{
int p,q;
cout<<"Enter values”;
cin>>p>>q;
Test ob1(p,q);
Test ob2(ob1);
cout<<”\n Values of object 1:”;
ob1.show();
cout<<”\n Values of object 2:”;
ob2.show();
return 0;
}
6. Write a C++ program to implement operator overloading
#include<iostream.h>
class negate
{
int x,y;
public:
void read()
{
void show()
{
cout<<"\n x="<<x;
cout<<"\n y="<<y;
}
void operator-()
{
x=-x;
y=-y;
}
};
int main()
{
negate ob;
ob.read();
cout<<"\n Before negation:";
ob.show();
-ob;
cout<<"\n After negation:";
ob.show();
return 0;
}
7. Write a C++ program to implement single level inheritance
#include<iostream.h>
class Employee
{
protected:
int empid;
char name[20];
char desig[20];
public:
void get()
{
cout<<"\nEnter empid, name and designation";
cin>>empid>>name>>desig;
}
};
class Salary : public Employee
{
protected:
float basic,DA,TA,HRA,PF,Netsal;
public:
void read()
{
cout<<"\nEnter the basic salary ";
cin>>basic;
}
void calculate()
{
DA=basic*20/100;
TA=basic*15/100;
HRA=basic*10/100;
PF=basic*12/100;
Netsal=basic+DA+TA+HRA-PF;
}
void put()
{
cout<<"\n Empid :"<<empid;
cout<<"\n Name :"<<name;
cout<<"\n Designation :"<<desig;
cout<<"\n Basic salary :"<<basic;
cout<<"\n DA :"<<DA;
cout<<"\n HRA :"<<HRA;
cout<<"\n TA :"<<TA;
cout<<"\n PF :"<<PF;
cout<<"\n Net salary :"<<Netsal;
}
};
int main()
{
Salary ob;
ob.get();
ob.read();
ob.calculate();
ob.put();
return 0;
}