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

C++_lab_4

Uploaded by

Tanishq Kaushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

C++_lab_4

Uploaded by

Tanishq Kaushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q1- Write a program to showcase the working of an Inline function.

Algorithm: First, we define a function with an inline keyword. Then, we call the function in
the main function.

#include <iostream>

using namespace std;

inline int divide(int a, int b) {

return (a / b);

int main() {

int x = 10;

int y = 2;

int result = divide(x, y);

cout << "The division of " << x << " and " << y << " is " << result << endl;

return 0;

Q2- Write a program to call functions using class objects as arguments.

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>

using namespace std;

class Complex {

private:

Tanisqh Kaushik A2305223381 ES203


int real;

int imaginary;

public:

void setData();

void printData();

void addNumber(Complex, Complex);

};

void Complex :: setData() {

cout << "Real Part: ";

cin >> real;

cout << "Imaginary part: ";

cin >> imaginary;

void Complex :: printData() {

cout << real << "+i" << imaginary << endl;

void Complex :: addNumber(Complex a, Complex b) {

real = a.real + b.real;

imaginary = a.imaginary + b.imaginary;

int main() {

Complex c1, c2, c3;

Tanisqh Kaushik A2305223381 ES203


c1.setData();

c2.setData();

cout << "First ";

c1.printData();

cout << "Second ";

c2.printData();

c3.addNumber(c1,c2);

cout << "Sum of the two complex numbers is: ";

c3.printData();

return 0;

Q3- WAP to return objects in a class function call

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>

using namespace std;

class Time {

private:

int hour;

int min;

Tanisqh Kaushik A2305223381 ES203


int sec;

public:

void getTime() {

cout << "hour: ";

cin >> hour;

cout << "minutes: ";

cin >> min;

cout << "seconds: ";

cin >> sec;

};

void printTime() {

cout << "hour: " << hour << " minutes: " << min << " seconds: " << sec << endl;

};

Time addTime(Time a, Time b) {

Time c;

c.hour = a.hour + b.hour;

c.min = a.min + b.min;

c.sec = a.sec + b.sec;

return c;

};

int main() {

Time c1, c2, c3, res;

c1.getTime();

Tanisqh Kaushik A2305223381 ES203


c2.getTime();

cout << "First ";

c1.printTime();

cout << "Second ";

c2.printTime();

res = c3.addTime(c1, c2);

cout << "Sum is: ";

res.printTime();

return 0;

Q4- Write a program to show the working of friend function.

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>

using namespace std;

class Square;

class Rectangle {

private:

Tanisqh Kaushik A2305223381 ES203


int width = 5;

int height = 5;

public:

friend void Area(Rectangle, Square);

};

class Square {

private:

int side = 5;

public:

friend void Area(Rectangle, Square);

};

void Area(Rectangle r, Square s) {

cout << "Rectangle: " << r.width * r.height << endl;

cout << "Sqaure: " << s.side * s.side << endl;

int main() {

Rectangle rect;

Square sq;

Area(rect, sq);

return 0;

Tanisqh Kaushik A2305223381 ES203

You might also like