You are on page 1of 9

Experiment No.

(PART B : TO BE COMPLETED BY STUDENTS)

Part B

(Students must submit the soft copy as per following segments within two hours of
the practical. The soft copy must be uploaded on the Blackboard or emailed to the
concerned lab in charge faculties at the end of the practical in case the there is no Black
board access available)

Roll No. A008 Name: Pronoy Debdas


Class: MCA Batch: B1
Date of Experiment: 10/10/16 Date of Submission: 24/10/16
Grade: A+

B.1 Software Code written by student and Input and Output:

P1.

Code:
#include<iostream>

using namespace std;

class number

int num;

public:

number(int n)

num=n;

void operator ++();

void display()
{

cout<<"\n\nNumber="<<num;

};

void number::operator++()

num=num+1;

int main()

number ob(6);

cout<<"Before increment: ";

ob.display();

++ob;

cout<<"\n\nAfter increment: ";

ob.display();

//ob++;

//ob.display();

return 0;

Output:
P2.

Code:
#include<iostream>

using namespace std;

class box

int x,y,z;

public:

box(){}

box(int a,int b, int c)

x=a;

y=b;

z=c;

box operator +(box b)

box a;

a.x=x+b.x;

a.y=y+b.y;
a.z=z+b.z;

return a;

void display()

cout<<"\n\tx="<<x<<"\n\ty="<<y<<"\n\tz="<<z;

};

int main()

box ob1(6,7,8), ob2(9,10,11);

box ob3=ob1+ob2;

cout<<"ob1 : ";

ob1.display();

cout<<"\n\nob2 : ";

ob2.display();

cout<<"\n\nob3 : ";

ob3.display();

return 0;

Output:
P3.

Code:
#include<iostream>

using namespace std;

class Dollars

double Dollar;

public:

Dollars()

Dollar =0.0;

Dollars(double x)

Dollar = x;

display();
}

void display()

cout<<"\n\nAfter Conversion: ";

cout<<"\n\nValue in Dollars= $"<<Dollar;

};

class Rupees

double Rupee;

public:

Rupees()

cout<<"Enter Rupees: ";

cin>>Rupee;

Rupees(double a)

Rupee=a;

cout<<"\n\n"<<Rupee<<"\n\n";

operator Dollars()

display();

return (Rupee/66.58);
}

void display()

cout<<"\nValue in Rupees= Rs"<<Rupee;

};

int main()

int R;

cout<<"Enter Rupees: ";

cin>>R;

Rupees q=R;

Dollars ob=q;

return 0;

Output:
B.2 Conclusion:
Learnt to implement static or compile time polymorphism using operator
overloading.

Learnt how to overload unary and binary operators.

Learnt how to implicitly and explicitly do type conversion in C++.

Also learnt how to convert data types using casting operators.

B.3 Question of Curiosity


(To be answered by student based on the practical performed and learning/observations)

Tick the correct option:

1. Which of the following operators cant be overloaded?


a) ::
b) +
c) -
d) []

a) ::

2. How to declare operator function?


a) operator operator sign
b) operator
c) operator sign
d) None of the mentioned

a) operator operator sign.

3. Which of the following statements is NOT valid about operator overloading?


a) Only existing operators can be overloaded.
b) Overloaded operator must have at least one operand of its class type.
c) The overloaded operators follow the syntax rules of the original operator.
d) None of the mentioned

d) None of the mentioned.

4. State true or false:


1. Compiler does not support automatic type conversions for the user defined data
types.

True. User defined data types cannot be implicitly converted, a casting operator
must be defined in the source class or a constructor with an object of source type
class must be defined in the destination class.

2. Casing operator must be a class member


True.

5. What is a return type of casting operator?

It depends on what type the casting is being done to. It can be a user defined return type
or a pre-defined return type too.

You might also like