0% found this document useful (0 votes)
5 views15 pages

Cpp Lab Programs

The document contains multiple C++ programs demonstrating various programming concepts including quadratic equations, employee management, student details, function overloading, classes and objects, operator overloading, friend functions, string comparison, constructors and destructors, matrix multiplication, inheritance, recursion, templates, file handling, and exception handling. Each program is self-contained and illustrates a specific feature or functionality of C++. The code snippets are structured to prompt user input and display results accordingly.

Uploaded by

mmohanm2323
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)
5 views15 pages

Cpp Lab Programs

The document contains multiple C++ programs demonstrating various programming concepts including quadratic equations, employee management, student details, function overloading, classes and objects, operator overloading, friend functions, string comparison, constructors and destructors, matrix multiplication, inheritance, recursion, templates, file handling, and exception handling. Each program is self-contained and illustrates a specific feature or functionality of C++. The code snippets are structured to prompt user input and display results accordingly.

Uploaded by

mmohanm2323
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/ 15

#include<iostream>

#include<cmath>
using namespace std;
int main(){
float a,b,c,d,x1,x2;
cout<<"Enter the Three values:";
cin>>a>>b>>c;
d=b*b-4*a*c;
if(d>0){
x1=(-b+sqrt(d))/(2*a);
x1=(-b-sqrt(d))/(2*a);
cout<<"Real and Different Roots"<<endl;
cout<<"x1:"<<x1<<" "<<"x2:"<<x2<<endl;
} else if(d==0){
x1=-b/(2*a);
cout<<"Real and Equal Roots"<<endl;
cout<<"x1=x2="<<x1<<endl;
}else{
x1=-b/(2*a);
x2=sqrt(-d)/(2*a);
cout<<"Imaginary and Complex Roots"<<endl;
cout<<"x1:"<<x1<<"+"<<x2<<"i"<<endl;
cout<<"x2:"<<x1<<"-"<<x2<<"i"<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
class Employee{
int id,sal;
string name;
public:
void getdata(){
cout<<"Enter Id:";
cin>>id;
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Salary:";
cin>>sal;
}
void putdata(){
cout<<"ID: "<<id<<" Name: "<<name<<" Salary: "<<sal<<end
}
};
int main(){
int n;
cout<<"Enter Number of Employee:";
cin>>n;
Employee emp[n];
cout<<"--------Enter Employee Details--------\n";
for(int i=0;i<n;i++){
cout<<"Employee "<<i+1<<endl;
emp[i].getdata();
}
cout<<"\n\n--------Employee Details--------\n";
for(int i=0;i<n;i++){
emp[i].putdata();
}
return 0;
}
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n,age[10],marks[10];
string name[20];
cout<<"Enter the number of student:";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Enter Student detail: "<<i+1<<endl;
cout<<"Name:";
cin>>name[i];
cout<<"Age:";
cin>>age[i];
cout<<"Marks:";
cin>>marks[i];
}
cout<<endl;
cout<<"\n-----------------------------------\n";
cout<<left<<setw(15)<<"Name"<<setw(15)<<"Age"
<<setw(15)<<"Marks";
cout<<"\n-----------------------------------\n";
for(int i=0;i<n;i++){
cout<<left<<setw(15)<<name[i]<<setw(15)
<<age[i]<<setw(15)<<marks[i]<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
inline int add(int a,int b){
return a+b;
}
inline int add(int a,int b,int c){
return a+b+c;
}
int main(){
int a,b,c;
cout<<"Enter two values:";
cin>>a>>b;
cout<<"Sum:"<<add(a,b)<<endl;
cout<<"Enter Three values:";
cin>>a>>b>>c;
cout<<"Sum:"<<add(a,b,c)<<endl;
return 0;
}
#include<iostream>
using namespace std;
class Box{
float len,wid,h;
public:
void getdata(){
cout<<"Enter the values(Length-Width-Height):";
cin>>len>>wid>>h;
}
float cal(){
return len*wid*h;
}
void display(){
cout<<"Cuboid volume:"<<cal()<<endl;
}
};
int main(){
Box b;
b.getdata();
b.display();
return 0;
}
#include<iostream>
using namespace std;
class Op{
int a;
public:
void getdata(){
cout<<"Enter the Integer:";
cin>>a;
}
Op operator + (Op obj){
Op temp;
temp.a=a+obj.a;
return temp;
}
void display(){
cout<<"Sum:"<<a<<endl;
}
};
int main(){
Op a,b,c;
a.getdata();
b.getdata();
c=a+b;
c.display();
return 0;
}
#include<iostream>
using namespace std;
class B;
class A{
int x;
public:
A() { x=10; }
friend void show(A,B);
friend class B;
};
class B{
int y;
public:
B() { y=20; }
friend void show(A,B);
void print(A a){
cout<<"\nFriend Class\n";
cout<<"A's private member:"<<a.x<<endl;
cout<<"B's private member:"<<y<<endl;
}
};
void show(A a,B b){
cout<<"Friend Function\n";
cout<<"A's Number:"<<a.x<<endl;
cout<<"B's Number:"<<b.y<<endl;
}
int main(){
A a;
B b;
show(a,b);
b.print(a);
return 0;
}
#include<iostream>
using namespace std;
class stringmatch{
public:
void check(string s1,string s2){
if(s1 == s2){
cout<<"Both strings are Equal"<<endl;
}else{
cout<<"Both strings are not Equal"<<endl;
}
}
};
int main(){
string a,b;
cout<<"Enter the First String:";
cin>>a;
cout<<"Enter the Second String:";
cin>>b;
stringmatch m;
m.check(a,b);
return 0;
}
#include<iostream>
using namespace std;
class student{
public:
student(){
cout<<"Constructor Called: Object Created!!"<<endl;
}
~student(){
cout<<"Destructor Called: Object Destroyed!!"<<endl;
}
void display(){
cout<<"Hey! I am a Programmer..."<<endl;
}
};
int main(){
student s1;
s1.display();
return 0;
}
#include<iostream>
using namespace std;
int main(){
int a[2][2],b[2][2],ans[2][2];
cout<<"Enter the Elements in A Matrix(2X2):";
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
cin>>a[i][j];
}
}
cout<<"Enter the Elements in B Matrix(2X2):";
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
cin>>b[i][j];
}
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
ans[i][j]=0;
for(int k=0;k<2;k++){
ans[i][j]+=a[i][k]*b[k][j];
}
}
}
cout<<"Multiplication Matrix:\n";
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
class A{
public:
int a,b;
void getdata(){
cout<<"Enter Two Values:";
cin>>a>>b;
}
};
class B{
public:
void operation(int a,int b){
cout<<"Addition:"<<a+b<<endl;
cout<<"Subtraction:"<<a-b<<endl;
cout<<"Multiplication:"<<a*b<<endl;
cout<<"Division:"<<a/b<<endl;
}
};
class C:public A,public B{
public:
void cal(){
getdata();
operation(a,b);
}
};
int main(){
C obj;
obj.cal();
return 0;
}
#include<iostream>
using namespace std;
int factorial(int n){
if(n==0 || n==1){
return 1;
}else{
return n*factorial(n-1);
}
}
int main(){
int n;
cout<<"Enter the Interger:";
cin>>n;
int ans=factorial(n);
cout<<"Factorial:"<<ans<<endl;
}
#include<iostream>
using namespace std;
template <typename T>
void sort(T arr[],int n){
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(arr[i]>arr[j]){
T temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
int main(){
int n,arr[100];
cout<<"Enter Number of Elements:";
cin>>n;
cout<<"Enter "<<n<<" numbers:";
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"\nAfter Sorting:";
sort(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main(){
fstream file;
file.open("data.txt",ios::out);
cout<<"File Created Successfully!!"<<endl;
cout<<"File name: data.txt\n";
file<<"Welcome to Cpp program...";
file.close();
file.open("data.txt",ios::in);
string l;
while(getline(file,l)){
cout<<"file content:\n";
cout<<l<<endl;
}
file.close();
return 0;
}
#include<iostream>
using namespace std;
int main(){
int a,b;
cout<<"Enter two numbers:";
cin>>a>>b;
try{
if(b==0){
throw "Divison By Zero is not allowed!!";
}else cout<<"Divison:"<<a/b<<endl;
}catch(const char*msg){
cout<<"Exception Caught:"<<msg<<endl;
}
return 0;
}

You might also like