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

C++(OOPs)1

Object Oriented Programming Notes

Uploaded by

Abcd Efgh
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)
8 views

C++(OOPs)1

Object Oriented Programming Notes

Uploaded by

Abcd Efgh
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/ 14

Program-1

Aim:- Write a program for multiplication of matrices using OOP.

#include <iostream>
#include <iomanip>
using namespace std;

class matrix
{
int **a,l1,l2;
public:
void initialize_matrix(int m,int n)
{
l1=m;
l2=n;
a = new int*[m];
for(int p=0;p<m;p++)
{
a[p] = new int[n];
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=0;
}
}
}

void display_matrix()
{
for(int i=0;i<l1;i++)
{
cout<<" ";
for(int j=0;j<l2;j++)
{
cout<<a[i][j]<<setw(5);
}
cout<<"\n";
}
}

void enter_matrix()
{
for(int i=0;i<l1;i++)
{
for(int j=0;j<l2;j++)
{
cin>>a[i][j];
}
}
}

int multiply_matrix(matrix m1, matrix m2)


{
if(m1.l2!=m2.l1)
{
cout<<"\n\n Matrix multiplication not possible\n";
return 0;
}
else
{
for(int i=0;i<m1.l1;i++)
{
for(int j=0;j<m2.l2;j++)
{
for(int k=0;k<m1.l2;k++)
{
a[i][j]+=m1.a[i][k]*m2.a[k][j];
}
}
}
return 1;
}
}
};

int main()
{
cout<<"\n\n Program for multiplication of two matrices using oop";
cout<<"\n ^^^^^^^ ^^^ ^^^^^^^^^^^^^^ ^^ ^^^ ^^^^^^^^ ^^^^^ ^^^";

matrix m1,m2,m3;
int row1,col1,row2,col2;

cout<<"\n\n Enter the rows and columns of first matrix - ";


cin>>row1>>col1;
m1.initialize_matrix(row1,col1);

cout<<"\n Enter the rows and columns of second matrix - ";


cin>>row2>>col2;
m2.initialize_matrix(row2,col2);

cout<<"\n Enter the elements of first matrix - ";


m1.enter_matrix();

cout<<"\n Enter the elements of second matrix - ";


m2.enter_matrix();

cout<<"\n First Matrix - \n\n";


m1.display_matrix();

cout<<"\n Second Matrix - \n\n";


m2.display_matrix();

cout<<"\n\n Multiplying both matrices...";


m3.initialize_matrix(row1,col2);

if(m3.multiply_matrix(m1,m2))
{
cout<<"\n\n Resultant matrix - \n\n";
m3.display_matrix();
}

return 0;
}
Program-2
Aim:- Using the concept of function overloading, write function for
calculating area of triangle, circle and rectangle.

#include<iostream>
using namespace std;

int area(int);
int area(int,int);
float area(float);
float area(float,float);

int main()
{
int s,l,b;
float r,bs,ht;

cout<<"Enter side of a square:";


cin>>s;

cout<<"Enter length and breadth of rectangle:";


cin>>l>>b;

cout<<"Enter radius of circle:";


cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;

cout<<"\nArea of square is "<<area(s);


cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(bs,ht);
}

int area(int s)
{
return(s*s);
}

int area(int l,int b)


{
return(l*b);
}

float area(float r)
{
return(3.14*r*r);
}

float area(float bs,float ht)


{
return((bs*ht)/2);
}
Program-3
Aim:- Write a program to generate Febonacci series using copy
constructor.

#include<iostream>
using namespace std;

class fibonacci
{
long int a,b; //data members
public:
fibonacci() //special member function constructor
{
a=-1;
b=1;

void fibseries(int n) //member function


{
int i,next;
cout<<"\n Resultant fibonacci series";
cout<<"\n-----------------------------\n";
for(i=0;i<n;i++)
{
next=a+b; //Expression
cout<<next<<endl; //To print the fibseries
a=b;
b=next;
}

}
};

int main()
{
fibonacci f;
int n;

cout<<"\n Fibonacci series \n";


cout<<"\n Enter the range = ";
cin>>n;

f.fibseries(n); //Accessing the members l function fibseries () using Dot (.)


operator
return 0;
}
Program-4
Aim:- Write a program to demonstrate the use of special functions,
constructor and destructor in the class template. The program is
used to find the bigger of two entered numbers.

#include <iostream>
using namespace std;

// Number class
class Number
{
int N;
public:

// Default constructor
Number()
{
N = 0;
}

// Parameterised constructor
Number(int X)
{
N = X;
}
int max(Number A)
{
if (N > A.N)
return N;
else
return A.N;
}
// Destructor
~Number()
{
cout << "Destructor working.\n";
}
};
int main()
{
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;

// Initialise two Number objects with constructor


Number A(a);
Number B(b);
cout << "Larger of two numbers is: " << A.max(B) <<
"\n";

return 0;
}

You might also like