CPP External Oral Programs
//Implementation of construcor and destructour
#include<iostream>
using namespace std;
class Test
{
public:
Test()
{
cout<<"\n This line is constructed By Constructor";
}
~Test()
{
cout<<"\n Destructor Destruct line created by counstructor";
}
};
main()
{
Test t,t1;
return 0;
}
// 2.2 Parameterised constructor
#include <iostream>
using namespace std;
class Wall {
private:
double length;
double height;
public:
Wall(double len, double hgt) {
length = len;
height = hgt;
}
double Area() {
return length * height;
}
};
int main() {
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
cout << "Area of Wall 1: " << wall1.Area() << endl;
cout << "Area of Wall 2: " << wall2.Area();
return 0;
}
//3function overloading
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
// Driver code
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}
// 4,13 unary operator Overloading
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<"The Count after unary operator overloading: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
//5 Binary operator overloading
#include <iostream>
using namespace std;
class A
{
int x;
public:
A(){}
A(int i)
{
x=i;
}
void operator+(A);
void display();
};
void A :: operator+(A a)
{
int m = x+a.x;
cout<<"The result of the addition after binary operator overloading : "<<m;
}
int main()
{
A a1(5);
A a2(4);
a1+a2;
return 0;
}
// 6single inheritance
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
//7 Multiple inheritance
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A,public B
{
public:
void display()
{
std::cout << "The value of a is : " <<a<< std::endl;
std::cout << "The value of b is : " <<b<< std::endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
}
//8.Multilevel inh
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
//9 Exception Handling
#include <iostream>
using namespace std;
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
//11 percentage
#include<iostream>
using namespace std;
int main()
{
int s1, s2, s3, s4, s5, sum, total = 500;
float per;
cout<<"\nEnter marks of 5 subjects : ";
cin>>s1>>s2>>s3>>s4>>s5;
sum = s1 + s2 + s3 + s4 + s5;
per = (sum * 100) / total;
cout<<"\nStudent Percentage Is : "<< per<<endl;
return (0);
}
// 12 vowel or not
#include <iostream>
using namespace std;
int main() {
char c;
bool isLowercaseVowel, isUppercaseVowel;
cout << "Enter an alphabet: ";
cin >> c;
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (!isalpha(c))
printf("Error! Non-alphabetic character.");
else if (isLowercaseVowel || isUppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
return 0;
}
14. Inline function
#include<iostream>
using namespace std;
inline int cube(int a)
{
return a*a*a;
};
int main()
{
cout<<"The Cube of 3 is: "<<cube(3);
return 0;
}
//15.Virtual base class
#include <iostream>
using namespace std;
class A {
public:
int number;
A() // constructor
{
number = 10;
}
};
class B : public virtual A {};
class C : public virtual A {};
class D : public B, public C{};
int main()
{
D object; // object is created for class d
cout << "number = " << object.number << endl;
return 0;
}
//16.class templates
#include <iostream>
using namespace std;
template <class T>
class myclass {
T a, b;
public:
myclass (T first, T second)
{a=first; b=second;}
T max();
};
T myclass<T>::max()
{
return (a>b? a : b);
}
int main () {
myclass <int> myobject (100, 75);
cout<<"Maximum of 100 and 75 = "<<myobject.max()<<endl;
myclass<char> mychobject('A','a');
cout<<"Maximum of 'A' and 'a' = "<<mychobject.max()<<endl;
return 0;
}
Comments
Post a Comment