C++_lab_4
C++_lab_4
Algorithm: First, we define a function with an inline keyword. Then, we call the function in
the main function.
#include <iostream>
return (a / b);
int main() {
int x = 10;
int y = 2;
cout << "The division of " << x << " and " << y << " is " << result << endl;
return 0;
Algorithm: First, we define the class Complex with private members, real and imaginary, and
public members such as setData(), printData() and addNumber(). Then, we create two objects
and pass them as arguments in a third separate object.
#include <iostream>
class Complex {
private:
int imaginary;
public:
void setData();
void printData();
};
int main() {
c2.setData();
c1.printData();
c2.printData();
c3.addNumber(c1,c2);
c3.printData();
return 0;
Algorithm: Define class Time along with private and public members. Then define a function
with parameter as class time objects. Further, create two objects and pass them as arguments
in a third separate object and return the object.
#include <iostream>
class Time {
private:
int hour;
int min;
public:
void getTime() {
};
void printTime() {
cout << "hour: " << hour << " minutes: " << min << " seconds: " << sec << endl;
};
Time c;
return c;
};
int main() {
c1.getTime();
c1.printTime();
c2.printTime();
res.printTime();
return 0;
Algorithm: First, we define two classes Rectangle and Square. These two are going to have
the same friend function Area. Then we define an object in both classes and call the friend
function.
#include <iostream>
class Square;
class Rectangle {
private:
int height = 5;
public:
};
class Square {
private:
int side = 5;
public:
};
int main() {
Rectangle rect;
Square sq;
Area(rect, sq);
return 0;