Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Assignment 9 CPP

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assignment 9 CPP

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment 9

Q1) Write a complete program demonstrating the use of following things:-


(a)Friend Function (b) Overloading the + binary operator
(c)Copy Constructor (d) Overloading the unary – operator.
Ans1) The program for above given question is:
#include <iostream>
using namespace std;

// Forward declaration of the class


class Number;

// Friend function declaration


void display(const Number& num);

// Class definition
class Number {
private:
int value;

public:
// Default constructor
Number() : value(0) {}

// Parameterized constructor
Number(int val) : value(val) {}

// Copy constructor
Number(const Number& other) {
value = other.value;
}

// Overloading the + operator


Number operator+(const Number& other) {
Number result;
result.value = value + other.value;
return result;
}

// Overloading the unary - operator


Number operator-() {
Number result;
result.value = -value;
return result;
}

// Friend function definition


friend void display(const Number& num);
};

// Friend function implementation


void display(const Number& num) {
cout << "Value: " << num.value << endl;
}

int main() {
// Create two Number objects
Number num1(5);
Number num2(10);

// Display the initial values


cout << "Initial values:" << endl;
display(num1);
display(num2);

// Add the numbers using overloaded + operator


Number sum = num1 + num2;
cout << "Sum of num1 and num2:" << endl;
display(sum);

// Use the unary - operator


Number negative = -num1;
cout << "Negative value of num1:" << endl;
display(negative);
return 0;
}
Output:

Q2) What are the characteristics of constructor function.


Ans2) A constructor is a special member function and it has the following
charateristics-
1. Should be Public
2. Invoke automatically when a new object is created (instantiated).
3. Initialize data members.
4. Same name as class
5. No return type,not even void
6. Constructors overloading.(Overloading is a phenomenon where the name same
can be used but different parameters are passed.)
7. Can not be inherited.
8. Constructors can not be virtual.
Q3) What are the characteristics of destructor function.
A3) A destructor is a special member function of a class that is responsible for
cleaning up the resources allocated by an object when it is destroyed or goes out
of scope. Some characteristics are:
1. Same name as class name preceded by a tilde(~).
2. No return type.
3. No parameters.
4. Implicitly generated if not defined.
5. Releases resources, deallocates memory.
6. Called in reverse order of constructors.
7. Should handle exceptions appropriately.
8. Virtual destructors for base classes.
9. Accessibility: public,private, or protected.
Q4) What are the rules for operator overloading.
A4) Operator overloading allows existing operators to be redefined for custom types.
Rules include defining operator functions as member or non- member functions,
using appropriate parameters and considering the commutative nature of certain
operators. Some rules are:
1. Overloaded operators must be non static member functions or global functions.
2. Overloaded operators cannot change their precedence or associativity.
3. Overloaded operators can be implemented as member functions or non-
member functions.
4. Overloaded operators must be defined with appropriate parameters and return
types.
5. Some operators have a specific syntax and behaviour that must be followed
when overloading.
6. Overloaded operators cannot create new operators or change the basic
behaviour of existing ones.
Q5) What are the types of inheritance. Write a program for single
inheritance.
A5) The mechanism of deriving a new class from an old one is called inheritance
(or derivation). The old class is referred to as the base class is referred to as the
base class and the new one is called the derived class or subclass.
There are three types of inheritance:
1. Single Inheritance: A derived class inherits properties and behaviours
from a single base class.
2. Multiple Inheritance: A derived class inherits properties and behaviours
from multiple base class.
3. Multilevel Inheritance: A derived class inherits propertied and behaviours
from a base class, which itself inherits from another base class.
A program for single inheritance is shown below:
#include <iostream>
using namespace std;

// Base class
class Shape {
protected:
int width;
int height;
public:
void setWidth(int w) {
width = w;
}

void setHeight(int h) {
height = h;
}
};

// Derived class
class Rectangle : public Shape {
public:
int getArea() {
return width * height;
}
};

int main() {
Rectangle rect;

// Set the dimensions


rect.setWidth(5);
rect.setHeight(7);

// Calculate and print the area


cout << "Area: " << rect.getArea() << endl;

return 0;
}

Output:

Q6) Write a program for function overloading (compile time polymorphism).


A6) A program showing function overloading (compile time polymorphism) is shown
below:
#include <iostream>
using namespace std;

// Function to add two integers


int add(int a, int b) {
return a + b;
}

// Function to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

// Function to add two floating-point numbers


float add(float a, float b) {
return a + b;
}

int main() {
int num1 = 5, num2 = 10, num3 = 15;
float num4 = 2.5, num5 = 3.7;

// Call the overloaded functions


int result1 = add(num1, num2);
int result2 = add(num1, num2, num3);
float result3 = add(num4, num5);

// Print the results


cout << "Result 1: " << result1 << endl;
cout << "Result 2: " << result2 << endl;
cout << "Result 3: " << result3 << endl;

return 0;
}
Output:

You might also like