Some important programs part 5
#include <iostream>
using namespace std;
int main()
{
int *p; try
{
p = new int;
}
catch (bad_alloc xa)
{
cout << "Allocation Failure \n";
}
*p = 1000;
cout << "At " << p << " ";
cout << "is the value " << *p << "\n";
delete p;
}
2.To Implement the use of unary operator overloading
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
Count() : value(4) {}
void operator ++ () {
++value;
}
void operator ++ (int) {
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main()
{ Count count1;
count1++;
count1.display();
++ count1;
count1.display();
return 0;
}
3. To implement the use of Binary operator
overloading
#include<iostream>
using namespace std;
class overloading
{
int value;
public:
void setValue(int temp)
{
value = temp;
}
overloading operator + (overloading ob)
{
overloading t;
t.value=value+ob.value;
return(t);
}
void display()
{
cout<<value<<endl;
}
};
int main()
{
overloading obj1 , obj2 , result;
int a , b;
cout<<"Enter the value of Complex Numbers a,b:";
cin>>a>>b;
obj1.setValue(a);
obj2.setValue(b); result = obj1 + obj2;
cout<<"Input Values: \n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
return 0;
}
4.To implement the assignment and comparison of two strings using binary
operator overloading
#include<iostream>
#include<string.h>
using namespace std;
class String
{
public:
char str[20];
public:
void accept_string()
{
cout<<"\n Enter String : ";
cin>>str;
}
void display_string()
{
cout<<str;
}
String operator+(String x)
{
String s;
strcat(str,x.str);
strcpy(s.str,str);
return s;
}
};
int main()
{
String str1, str2, str3;
str1.accept_string();
str2.accept_string();
cout<<"\n\n First String is : ";
str1.display_string();
cout<<"\n\n Second String is : ";
str2.display_string();
str3=str1+str2;
cout<<"\n\n Concatenated String is : ";
str3.display_string();
return 0;
}
5. To implement the use of single private and public inheritance
#include<iostream>
using namespace std;
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
cout<<"\nEnter the employee number : ";
cin>>eno;
cout<<"Enter the employee name : ";
cin>>name;
cout<<"Enter the designation : ";
cin>>des;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<<"Enter the basic pay : ";
cin>>bp;
cout<<"Enter the Humen Resource Allowance : ";
cin>>hra;
cout<<"Enter the Dearness Allowance : ";
cin>>da;
cout<<"Enter the Profitablity Fund : ";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np<<"\n";
}
};
int main()
{
int i,n; char ch;
salary s[10];
cout<<"Enter the number of employee : ";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for(i=0;i<n;i++)
{
s[i].display();
}
return 0;
}
6. To implement the use of multiple inheritance
#include<iostream>
using namespace std;
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
public:
int sm; // sm = Sports mark public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot; cout<<"\n\tAverage : "<<avg;
}
};
int main()
{
statement obj;
obj.get();
obj.getsm();
obj.display();
return 0;
}
0 Comments