Search This Blog

Showing posts with label classes. Show all posts
Showing posts with label classes. Show all posts

Tuesday, 2 February 2016

Array of classes

#include<iostream>
#include<iomanip>

using namespace std;

class EMPLOYEE
{
string name;
int age;
public:
void getdata();
void putdata();
};

void EMPLOYEE::getdata()
{
cout << "\t\tEnter name of the employee;";
cin >> name;
cout << "\n\t\tEnter age of the employee:";
cin >> age;
}

void EMPLOYEE::putdata()
{
cout << "\t\t-------------------------" << endl;
cout << "\t\t" << setw(15) << left << "Name - " << name << endl;
cout << "\t\t" << setw(15) << left << "Age - " << age << endl;
cout << "\t\t-------------------------" << endl;
}

int main()
{
int n;
cout << "\t\tEnter no. of employees:";
cin >> n;
EMPLOYEE emp[n];
for(int i=0;i<n;i++)
emp[i].getdata();
for(int i=0;i<n;i++)
emp[i].putdata();
return 0;
}

Demonstrating Static Functions and Static Members

 Practical 12


#include<iostream>
#include<iomanip>

using namespace std;

class STAT
{
static int a;
int b;
public:
void getdata();
static void display();
void display2();
};

void STAT::getdata()
{
cout << "\t\tEnter number : ";
cin >> b;
}

void STAT::display()
{
cout << "Static number is " << a++ << endl;
}

void STAT::display2()
{
cout << "Static number is " << a++ << endl; 
cout << "Non Static number is " << b << endl;
}

int STAT::a;

int main()
{
STAT a;
a.getdata();
STAT::display();
a.display2();
return 0;
}



output:



enter number : 2
static number is : 0
static number is 1
non static number is : 2

Roots of Quadratic equation using Classes

#include<iostream>
#include<math.h>

using namespace std;

class quadratic
{
int a,b,c,d;
float x1,x2;
public:
quadratic();
void roots();
};

quadratic::quadratic()
{
cout << "Enter value of x^2" << endl;
cin >> a;
cout << "Enter value of x^1" << endl;
cin >> b;
cout << "Enter value of x^0" << endl;
cin >> c;
}

void quadratic::roots()
{
d=(pow(b,2)-4*a*c);
if(d<0)
cout << "Roots are imaginary" << endl;
else if(d==0)
{
x1=x2=-b/(2*a);
cout << "Roots are real and equal for x = " << x1 << endl;
}
else
{
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
cout << "Roots are real and distinct and x1 = "<< x1 << " and x2 = " << x2 << endl;
}
}
int main()
{
quadratic a;
a.roots();
return 0;
}

Addition and Multiplication of matrices using Classes

#include<iostream>
#include<math.h>

using namespace std;

class matrix
{
int m,n,a[10][10];
public:
matrix();
matrix(int,int);
void putdata();
int sum(matrix* ,matrix*);
int mul(matrix* ,matrix*);
void display();
};

matrix::matrix(){}

void matrix::putdata()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout << "Enter value of element in " << i << " row  and " << j << " column:" << endl;
cin >> a[i][j];
}
}
}

matrix::matrix(int a,int b)
{
m=a,n=b;
}

int matrix::sum(matrix *m1,matrix *m2)
{
if(m1->m != m2->m || m1->n != m2->n)
{
cout << "Matrices are of different order and cannot be added" << endl;
return(0);
}
else
{
m=m1->m;
n=m1->n;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
a[i][j]=m1->a[i][j]+m2->a[i][j];
}
return(1);
}
}

int matrix::mul(matrix *m1,matrix *m2)
{
if(m1->n != m2->m)
{
cout << "Matrices cannot be multiplied" << endl;
return(0);
}
else
{
m=m1->m;
n=m2->n;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=0;
for(int k=0;k<m1->n;k++)
a[i][j]=m1->a[i][k]*m2->a[k][j]+a[i][j];
}
}
return(1);
}
}

void matrix::display()
{
for(int i=0;i<m;i++)
{
cout << "| ";
for(int j=0;j<n;j++)
cout << a[i][j] << " ";
cout << "|" << endl;
}
}

int main()
{
int m,n,flag=-1,ch;
cout << "Enter no. of rows of matrix A" << endl;
cin >> m;
cout << "Enter no. of columns of matrix A" << endl;
cin >> n;
matrix a(m,n);
a.putdata();
cout << "Enter no. of rows of matrix B" << endl;
cin >> m;
cout << "Enter no. of columns of matrix B" << endl;
cin >> n;
matrix b(m,n);
b.putdata();
matrix c;
do
{
cout << "Enter choice " << endl 
<< "1.Sum " << endl
<< "2.Multiplication" << endl
<< "0.Exit" << endl;
cin >> ch;
switch(ch)
{
case 1:
flag=c.sum(&a,&b);
break;
case 2:
flag=c.mul(&a,&b);
break;
}
if(flag!=0)
c.display();
}while(ch!=0);
}



Finding Roots of Algebraic Equation using Bisection Method

#include<iostream>
#include<math.h>

using namespace std;

class Equation
{
int a[20],n;
double x,y,root,m;
public:
Equation();
void roots();
double solution(double t);
};

Equation::Equation()
{
cout << "\t\tEnter highest power of equation: ";
cin >> n;
cout << endl;
for(int i=n;i>=0;i--)
{
cout << "\t\tEnter coefficient of x^" << i << ":";
cin >> a[i];
cout << endl;
}
cout << "\t\tEnter interval for roots:";
cin >> x >> y;
cout << endl;
}

void Equation::roots()
{
if((solution(x)*solution(y))>0)
cout << "Roots does not exist in given interval (" << x << "," << y << ")" << endl;
else
{
if((y-x)<=0.00001)
cout << "Root is " << x << endl;
else
{
m=(x+y)/2;
if((solution(x)*solution(m))<0)
{
y=(x+y)/2;
roots();
}
else
{
x=(x+y)/2;
roots();
}
}
}
}

double Equation::solution(double t)
{
double x=0;
for(int i=0;i<=n;i++)
{
x=x+(a[i]*pow(t,i));
}
return x;
}

int main()
{
Equation a;
a.roots();
return 0;
}