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

Program of Oops

The document contains 6 programs demonstrating different C++ concepts: 1) A program to input and output a string 2) A program to calculate the average of 3 numbers input by the user 3) A program using a function to multiply two numbers 4) A program demonstrating the use of classes with member functions 5) A program using inline functions 6) A program demonstrating function overloading to calculate volumes of different shapes

Uploaded by

Ritu Kapur
Copyright
© Attribution Non-Commercial (BY-NC)
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)
39 views

Program of Oops

The document contains 6 programs demonstrating different C++ concepts: 1) A program to input and output a string 2) A program to calculate the average of 3 numbers input by the user 3) A program using a function to multiply two numbers 4) A program demonstrating the use of classes with member functions 5) A program using inline functions 6) A program demonstrating function overloading to calculate volumes of different shapes

Uploaded by

Ritu Kapur
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 13

//Program 1

//Program to enter a string

#include<iostream.h> //include header files

#include<conio.h> //include header files

using namespace std;

int main() //int type main function

char str[40];

cout << "Enter a string\n" << "\n" ; //input string

cin >> str;

cout << "\n" << "Entered string is: " << str << "\n"; //output string

getch();

return 0; //main function returning 0

system("pause");

}
Output 1:-
//Program 2

//Program to find the average of three numbers entered by the user

#include<iostream.h> //include header files

#include<conio.h> //include header files

using namespace std;

int main() //int type main function

int n1,n2,n3,avg;

cout << "Enter the three numbers\n" << "\n" ; //inputing the numbers

cin >> n1>> n2 >> n3;

avg=(n1+n2+n3)/3;

cout << "avg of " << n1 << ", " << n2 << " and " << n3 << " is : " << avg << "\n"; //result

getch();

return 0; //int main function returning 0

system("pause");

}
Output 2:-
//Program 3

//Program to multiply two numbers with the help of a function

#include<iostream.h> // include header files

#include<conio.h> // include header files

int multiply (int,int); // declaration of multiply (user defined) function

int main() // int type main function

int n1,n2,product;

cout << "Enter the two numbers to be multiplied\n" << "\n" ;

cin >> n1>> n2>>”\n”; // entering the numbers

product=multiply(n1,n2); // calling multiply function

cout << "product of " << n1 << " and " << n2 << " is : " << product << "\n"; // result

getch();

return 0;// main returning 0

system("pause");

//definition of multiply function

int multiply (int c,int d)

int e;

e=c*d; //multiplication of the numbers

return(e);

}
Output 3:-
//Program 4

//Program to write a simple c++ program using class

#include<iostream.h> //include header files

#include<conio.h> //include header files

using namespace std;

class room //class declaration

float width,height; //private data by default

int cost; //private data by default

public:

void getdata(float a,float b,int c); //prototype declaration

//member function defined inside the class

void info(void)

cout << "width: " << width << "\n";

cout << "height: " << height << "\n";

cout << "cost: " << cost << "\n";

};

//member function definition outside the class

void room :: getdata(float a,float b,int c)

width=a;//private variables directly used

height=b;

cost=c;

}
//int type main program

int main()

room x;//first object defined

cout << "rooms of first type " << "\n";

x.getdata(22.6,34.5,278); //calling member function

x.info(); //calling member function

room y; //second object defined

cout << "rooms of second type" << "\n";

y.getdata(12.6,21.4,123); //calling member function

y.info(); //calling member function

getch();

return 0; //main returning 0

system("pause");

}
Output 4:-
//Program 5

//Program to use inline functions

#include<iostream.h> //include header files

#include<conio.h> //include header files

//declaration and definition of inline functions

inline float mul(float a,float b)

return(a*b);

inline double div(double p,double q)

return(p/q);

//int main function

int main()

float a= 12.345;

float b= 9.82;

cout << “result of multiplication is: ” << mul(a,b) << "\n";

cout << “result of division is: ” << div(a,b) << "\n";

return 0; //function main returning 0

system(”pause”);

}
//Program 6

//Program to perform function overloading

#include<iostream.h>

using namespace std;

//Declaration (prototypes)

int volume(int);

double volume(double,int);

long volume(long,int,int);

int main()

cout << volume(10) << "\n";

cout << volume(2,5,8) << "\n" ;

cout << volume(100L,75,15) << "\n";

return 0;

system("pause");

//function definitions

int volume(int s) //cube

return(s*s*s);

double volume(double r, int h) //cylinder

return(3.14519*r*r*h);

long volume(long l,int b,int h) //rectangular box

{
return(l*b*h);

You might also like