0% found this document useful (0 votes)
367 views17 pages

Oops Through C++ Prgms

The document contains summaries of 14 C++ programs covering topics such as variable scope, function overloading, inheritance, exception handling, and more. Each program includes the code, inputs/outputs, and a brief 1-2 sentence description.

Uploaded by

Baswamy Cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
367 views17 pages

Oops Through C++ Prgms

The document contains summaries of 14 C++ programs covering topics such as variable scope, function overloading, inheritance, exception handling, and more. Each program includes the code, inputs/outputs, and a brief 1-2 sentence description.

Uploaded by

Baswamy Cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Exp1: Write a c++ program illustrating Variable scope

#include <iostream>
using namespace std;

// Global variable declaration:
int g = 20;

int main ()
{
// Local variable declaration:
int g = 10;

cout << g;

return 0;
}
O/P:
10

Exp2: Write a c++ program illustrating swapping integer values by reference

#include <iostream>
using namespace std;

// function declaration
void swap(int& x, int& y);

int main ()
{
// local variable declaration:
int a = 100;
int b = 200;

cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;

/* calling a function to swap the values.*/
swap(a, b);

cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;

return 0;
}

// function definition to swap the values.
void swap(int& x, int& y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */

return;
}

OUTPUT:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100


Exp3: Write a c++ program illustrating checking whether the number is even or odd
using ternary operator

#include<iostream.h>
#include<conio.h>

int main()
{
int a;
cout<<"Enter the Number : ";
cin>>a;
(a%2==0)?cout<<"Number is even":cout<<"Number is odd";

getch();
return 0;
}


OUTPUT:
enter The number: 42
Number is even

Exp4: Write a c++ program illustrating a program to find the roots of a quadratic
equation. Use switch statements to handle different values of discriminant (b
2
-4*a*c)





EXP10: Write a c++ program illustrating Function OVERLOADING

#include <iostream>
using namespace std;

class printData
{
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}

void print(double f) {
cout << "Printing float: " << f << endl;
}

void print(char* c) {
cout << "Printing character: " << c << endl;
}
};

int main(void)
{
printData pd;

pd.print(5);
pd.print(500.263);
pd.print("Hello C++");
return 0;
}


O/P:
Printing int: 5
Printing float: 500.263
Printing character: Hello C++




Exp12: Write a c++ program illustrateing inline functions

#include <iostream>
using namespace std;
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
// Main function for the program
int main( )
{
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}
O/P:


Exp13: Write a c++ program illustrating Friend functions

#include <iostream>
using namespace std;

class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
void Box::setWidth( double wid )
{
width = wid;
}
void printWidth( Box box )
{
cout << "Width of box : " << box.width <<endl;
}

int main( )
{
Box box;
box.setWidth(10.0);
printWidth( box );
return 0;
}
O/P:
Width of box : 10
Exp14: Write a c++ program illustrating EXCEPTION HANDELING

#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;
}
O/P: Division by zero condition!


Exp20: Write a c++ program illustrating CONSTRACTOR OVERLOADING


#include <iostream.h>
class Overclass
{
public:
int x;
int y;
Overclass() { x = y = 0; }
Overclass(int a) { x = y = a; }
Overclass(int a, int b) { x = a; y = b; }
};
int main()
{
Overclass A;
Overclass A1(4);
Overclass A2(8, 12);
cout << "Overclass A's x,y value:: " <<
A.x << " , "<< A.y << "\n";
cout << "Overclass A1's x,y value:: "<<
A1.x << " ,"<< A1.y << "\n";
cout << "Overclass A2's x,y value:; "<<
A2.x << " , "<< A2.y << "\n";
return 0;
}

o/p:



Exp21: Write a c++ program illustrating COPY CONSTRACTOR

#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // destructor

private:
int *ptr;
};

Line::Line(int len)
{
cout << "Normal constructor allocating ptr" << endl;
ptr = new int;
*ptr = len;
}

Line::Line(const Line &obj)
{
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
}

Line:: ~Line(void)
{
cout << "Freeing memory!" << endl;
delete ptr;
}
int Line::getLength( void )
{
return *ptr;
}

void display(Line obj)
{
cout << "Length of line : " << obj.getLength() <<endl;
}
int main( )
{
Line line(10);
display(line);
return 0;
};
}


Exp7: Write a c++ program illustrating sort integer numbers

#include<iostream.h>
#include<conio.h>
void exchange(int (&a)[],int &n);
int main()
{
int a[10],size;
clrscr();
cout<<"Enter the Array size : ";
cin>>size;
cout<<"Enter the Array elements :\n";
for(int i=0;i<size;i++)
cin>>a[i];
exchange(a,size);
cout<<"After sorting :\n";
for(i=0;i<size;i++)
cout<<a[i]<<endl;
getch();
return 0;
}
void exchange(int (&a)[],int &n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(a[i]<a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

OUTPUT:

Enter the Array size : 10
Enter the Array elements :
15
46
89
62
-5
-78
0
5
45
9
After sorting :
-78
-5
0
5
9
15
45
46
62
89


Exp17: Write a c++ program illustrating Virtual Function


#include <iostream>
using namespace std;

class Shape
{
protected:
double width, height;
public:
void set_data (double a, double b)
{
width = a;
height = b;
}
virtual double area() = 0;
};

class Rectangle: public Shape
{
public:
double area ()
{
return (width * height);
}
};

class Triangle: public Shape
{
public:
double area ()
{
return (width * height)/2;
}
};

int main ()
{
Shape *sPtr;

Rectangle Rect;
sPtr = &Rect;

sPtr -> set_data (5,3);
cout << "Area of Rectangle is " << sPtr -> area() << endl;

Triangle Tri;
sPtr = &Tri;

sPtr -> set_data (4,6);
cout << "Area of Triangle is " << sPtr -> area() << endl;
return 0;
}

OUTPUT:
Area of Rectangle is 15
Area of Triangle is 12

Exp26: Write a c++ program illustrating Inheritance (multiple, multilevel, Hybrid)

i) Multiple Inheritances
#include <iostream>

using namespace std;

// Base class Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

// Base class PaintCost
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};

// Derived class
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
};

int main(void)
{
Rectangle Rect;
int area;

Rect.setWidth(5);
Rect.setHeight(7);

area = Rect.getArea();

// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;

// Print the total cost of painting
cout << "Total paint cost: $" << Rect.getCost(area) << endl;

return 0;
}

OUTPUT:
Total area: 35
Total paint cost: $2450

ii) multilevel Inheritance


#include<iostream.h>
#include<conio.h>
class student
{
protected : int rollno;
public : void get_num();
void put_num();
};
void student::get_num()
{
cout<<"\nEnter the roll number:\t";
cin>>rollno;
}
void student::put_num()
{
cout<<"Rollnumber: "<<rollno;
}
class test:public student
{
protected : float sub1,sub2;
public:
void get_marks()
{
cout<<"\nEnter the sub1 marks: ";
cin>>sub1;
cout<<"\nEnter the sub2 marks: ";
cin>>sub2;
}
void put_marks()
{
cout<<"\nSub1="<<sub1;
cout<<"\nSub2="<<sub2;
}
};
class result : public test
{
float total;
public:
void display()
{
total=sub1+sub2;
put_num();
put_marks();
cout<<"\nTotal= "<<total;
}
};
int main()
{
clrscr();
result r;
r.get_num();
r.get_marks();
r.display();
getch();
return 0;
}
OUTPUT:
Enter the roll number: 11111
Enter the sub1 marks: 67
Enter the sub2 marks: 56
Rollnumber: 11111
Sub1=67
Sub2=56
Total= 123









iii) Hybrid Inheritance

Program:
#include<iostream.h>
#include<conio.h>
class arithmetic
{
protected:
int num1, num2;
public:
void getdata()
{
cout<<"For Addition:";
cout<<"\nEnter the first number: ";
cin>>num1;
cout<<"\nEnter the second number: ";
cin>>num2;
}
};
class plus:public arithmetic
{
protected:
int sum;
public:
void add()
{
sum=num1+num2;
}
};
class minus
{
protected:
int n1,n2,diff;
public:
void sub()
{
cout<<"\nFor Subtraction:";
cout<<"\nEnter the first number: ";
cin>>n1;
cout<<"\nEnter the second number: ";
cin>>n2;
diff=n1-n2;
}
};
class result:public plus, public minus
{
public:
void display()
{
cout<<"\nSum of "<<num1<<" and "<<num2<<"= "<<sum;
cout<<"\nDifference of "<<n1<<" and "<<n2<<"= "<<diff;
}
};
void main()
{
clrscr();
result z;
z.getdata();
z.add();
z.sub();
z.display();
getch();
}

OUTPUT:

For Addition:
Enter the First Number: 10

Enter the Second Number: 5

For subtraction:
Enter the First Number: 9

Enter the Second Number: 7

Sum of 10 and 5 =15
Difference of 9 and 7 =2



Exp8: Write a c++ program illustrating Factorial Using Recursion


#include<iostream>
#include<conio.h>

using namespace std;

//Function
long factorial(int);

int main()
{

// Variable Declaration
int counter, n;

// Get Input Value
cout<<"Enter the Number :";
cin>>n;

// Factorial Function Call
cout<<n<<" Factorial Value Is "<<factorial(n);

// Wait For Output Screen
getch();
return 0;
}

// Factorial recursion Function
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

Output
Enter the Number :6
6 Factorial Value Is 720

You might also like