Sie sind auf Seite 1von 11

SUMIT

07514802717
4th SEMESTER
EXPERIMENT 01

AIM – Write a program for multiplication of two matrices using OOP.

SOURCE CODE:
#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;

int main()

system("color f0");

system("cls");

int a[10][10],b[10][10],mult[10][10],r1,c1,r2,c2,i,j,k,sum=0;

cout<<"Enter number of rows and columns of 1st matrix: ";

cin>>r1>>c1;

cout<<"Enter number of rows and columns of 2nd matrix: ";

cin>>r2>>c2;

if(c1!=r2)

cout<<"Error! Matrices cannot be multiplied";

cout<<"\nColumn of 1st matrix not equal to rows of 2nd matrix"<<endl;

else

cout<<"Enter elements of 1st matrix:"<<endl;


SUMIT
07514802717
4th SEMESTER
for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

cin>>a[i][j];

cout<<"Enter elements of 2nd matrix:"<<endl;

for(i=0;i<r2;i++)

for(j=0;j<c2;j++)

cin>>b[i][j];

for(i=0;i<r1;i++)

for(j=0;j<c2;j++)

{ sum=0;

for(k=0;k<c1;k++)

sum+=a[i][k]+b[k][j];

mult[i][j]=sum;

cout<<"1st Matrix:\n";

for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

cout<<a[i][j]<<"\t";

cout<<endl;
SUMIT
07514802717
4th SEMESTER
}

cout<<"\n2nd Matrix:\n";

for(i=0;i<r2;i++)

for(j=0;j<c2;j++)

cout<<b[i][j]<<"\t";

cout<<endl;

cout<<"\nMultiplication of two matrix is:\n";

for(i=0;i<r1;i++)

for(j=0;j<c2;j++)

cout<<mult[i][j]<<"\t";

cout<<endl;

return 0;

}
SUMIT
07514802717
4th SEMESTER

OUTPUT:
SUMIT
07514802717
4th SEMESTER
EXPERIMENT 02

AIM – Write a program to find the greatest of two given numbers in


two different classes using friend function.

SOURCE CODE:
#include<iostream>

#include<stdlib.h>

using namespace std;

class A;

class B{

private:

int b;

public:

B(int y) {

b=y;}

friend int max_fun(A x,B y);

};

class A{

private:

int a;

public:

A(int b) {

a=b;}

friend int max_fun(A x,B y);


SUMIT
07514802717
4th SEMESTER
};

int max_fun(A x,B y) {

if(x.a>y.b)

return x.a;

else

return y.b;

int main()

system("color f0");

int m,n;

cout<<"Enter first and second number: ";

cin>>m>>n;

A obj(m);

B obj1(n);

int maxnum=max_fun(obj,obj1);

cout<<maxnum<<" is greater";

return 0;

OUTPUT:
SUMIT
07514802717
4th SEMESTER
VIVA QUESTIONS:
1. What is the difference between friend function and friend class?

Ans-> In short, one is a class and one is a function. For the function, just that
one function gets access to private members. For a class, the whole class and all
its functions get access to the private members of the befriended class. The
friend keyword is used to grant access to private data members.
2. What are the Advantages of using friend classes?

Ans-> A friend function has the following advantages :


1. Provides additional functionality which is kept outside the class.
2. Provides functions that need data which is not normally used by the class.
3. Allows sharing private class information by a non member function.

3. Give an example for the use of volatile keyword in c++?

Ans-> The volatile keyword is intended to prevent the compiler from applying
any optimizations on objects that can change in ways that cannot be
determined by the compiler.
volatile uint16_t x;

volatile uint8_t * p_reg;

uint8_t * p_reg = (uint8_t *) 0x1234; // peripheral registers

4. What is a container class?

Ans-> A container class is a class that is used to hold objects in memory or


external storage. A container class acts as a generic holder. A container class
has a predefined behaviour and a well-known interface.

5. What is function prototyping? What are its advantages?

Ans-> Function prototyping is a function declaration statement that tells the


compiler about the return type of the function and the number as well as type
of arguments required by the function at the time of calling it.
Advantage of function prototype :- It helps the compiler in determining whether
a function is called correctly or not. Each time when a function is called, its
alling statement is compared with its prototype. In case of any mismatch,
compiler reports an error.
SUMIT
07514802717
4th SEMESTER
EXPERIMENT 03

AIM – Write a program to perform addition of two complex numbers


using constructor overloading.

SOURCE CODE:
#include<iostream>

#include<stdlib.h>

using namespace std;

class Complex

int real;

int imag;

public:

Complex(){}

Complex(int r)

real=r;

imag=r;

Complex(int x,int y)

real=x;

imag=y;

void add(Complex t,Complex p)


SUMIT
07514802717
4th SEMESTER
{

real=t.real+p.real;

imag=t.imag+p.imag;

void display()

cout<<real<<" + "<<imag<<"i\n";

};

int main()

system("cls");

system("color f0");

Complex c;

Complex c1(2);

Complex c2(3);

cout<<"Before addition:\n";

cout<<"First object values: ";

c1.display();

cout<<"Second object values: ";

c2.display();

c.add(c1,c2);

cout<<"After addition:\nNew object values: ";

c.display();

}
SUMIT
07514802717
4th SEMESTER

OUTPUT:
SUMIT
07514802717
4th SEMESTER

VIVA QUESTIONS:
1. Explain Default constructor.

Ans-> A default constructor is a constructor that either has no parameters, or if


it has parameters, all the parameters have default values. If no user-defined
constructor exists for a class A and one is needed, the compiler implicitly
declares a default parameterless constructor A::A() .

2. When are the Global objects destroyed?

Ans-> Global objects are destroyed when the program terminates.

3. What is a virtual destructor? Explain the use of it.

Ans-> Deleting a derived class object using a pointer to a base class that has a
non-virtual destructor results in undefined behavior. To correct this situation,
the base class should be defined with a virtual destructor.

If your derived class destructor is virtual then objects will be destructed in a


order(firstly derived object then base ). If your derived class destructor is NOT
virtual then only base class object will get deleted(because pointer is of base
class "Base *myObj"). So there will be memory leak for derived object.

4. Difference between a copy constructor and an assignment operator.

Ans-> The fundamental difference between the copy constructor and


assignment operator is that the copy constructor allocates separate memory to
both the objects, i.e. the newly created target object and the source object. The
assignment operator allocates same memory location to the newly created target
object as well as source object.

5. What are the restrictions apply to constructors and destructors?

Ans-> The following restrictions apply to constructors and destructors:


Constructors and destructors do not have return types nor can they return
values. References and pointers cannot be used on constructors and
destructors because their addresses cannot be taken. Constructors cannot be
declared with the keyword virtual.

Das könnte Ihnen auch gefallen