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

C++ reduced list 2021

The document contains multiple C++ programs demonstrating various programming concepts including dynamic memory allocation, inline functions, function overloading, friend functions for swapping, parameterized and copy constructors, operator overloading, and single-level inheritance. Each program is structured with a main function and relevant class definitions to illustrate the specific concept. The examples provide a practical approach to understanding C++ features and their implementations.
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)
11 views

C++ reduced list 2021

The document contains multiple C++ programs demonstrating various programming concepts including dynamic memory allocation, inline functions, function overloading, friend functions for swapping, parameterized and copy constructors, operator overloading, and single-level inheritance. Each program is structured with a main function and relevant class definitions to illustrate the specific concept. The examples provide a practical approach to understanding C++ features and their implementations.
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/ 13

1.

Write a C++ program to implement dynamic memory


allocation using new and delete operators.

#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;
}

void area(int l, int b)


{
int a;
a=l*b;
cout<<"\n Area of rectangle is "<<a;
}

};
int main()
{
shape ob;
int s,l,b;
double r;

cout<<"\n Enter the side of a square";


cin>>s;

cout<<"\n Enter the radius of a circle";


cin>>r;

cout<<"\n Enter the length and breadth of a rectangle";


cin>>l>>b;

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()

cout<<"Enter values for x and y”;

cin>>x>>y;

void print()

cout<<"\n x= "<<x;

cout<<”\n y= “<<y;

friend void swap(Demo &ob);


};

void swap(Demo &ob)

int t;

t=ob.x;

ob.x=ob.y;

ob.y=t;

int main()

Demo ob;

ob read();

cout<<"\n Before swapping: ";

ob.print();

swap(ob);

cout<<"\n After swapping: ";

ob.print();

return 0;
};

5. Write a C++ program to implement parameterized and copy


constructor.

#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()
{

cout<<"\n Enter values for x and y ";


cin>>x>>y;
}

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;
}

You might also like