CS304 ASSIGNMENT 1 SOLUTION SPRING 2022

 


CS304 ASSIGNMENT 1 SOLUTION SPRING 2022 



Due Date: 1 June 2022

Total Marks: 20


Uploading Instructions:

Your assignment should be in .CPP format (Any other formats like scan images, PDF, zip, doc, rar and bmp etc. will not be accepted). 


Save your assignment with your ID (e.g. bc000000000.CPP).


No assignment will be accepted through email.



Question 1:


You are hired by XYZ company and being a software engineer your task is to develop the small calculator that deals with the complex number. Your responsibility is to write a program that add and subtract two complex number and to achieve this you have to create a class called Complex that has two data members real (int) and imag (int) to store the real and imaginary parts of the complex number. Implement the following tasks:

  • A default constructor that initializes the data members to 0.

  • A member function name get_data() to get the data from user.

  • A member function name display_data() to display the values of complex numbers.

  • A member function name add_complex() to add the two complex numbers using object as an argument.

  • A member function name sub_complex() to subtract the two complex numbers using object as an argument.

  • There should be copy constructor that copies an object. You have to create two objects and copy the data of one object to another object. 

Note: 

Enter the last digit of your VU ID as real part and second last digit as an imaginary part.

Definition of both the member function should be outside of the class. 

After running your program, the following screen should display.


Also, Check Latest Solution


ECO401 Assignment 1 Solution Spring 2022

CODE Solution:


// student Name: ------

// Student ID: -----


#include<iostream>

using namespace std;


class Complex

{

private:

int real, image;

public:

Complex(){

real = 0;

image = 0;

}

Complex(Complex &obj)

{

real = obj.real;

image = obj.image;

}

void get_data(){

cout<<"Enter complex number:"<<endl;

cout<<"Real = ";

cin>>real;

cout<<"Imaginary = ";

cin>>image;

}

void display_data(){

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

}

void add_Complex(Complex &obj);

void sub_Complex(Complex &obj);

};

int main(){

Complex obj;

obj.get_data();

cout<<"1st Complex number is : "<<endl;

obj.display_data();

Complex obj1(obj);

cout<<"2nd Complex number is : "<<endl;

obj1.display_data();

obj1.add_Complex(obj);

obj1.sub_Complex(obj);

}

void Complex::add_Complex(Complex &obj){

Complex temp;

temp.real = real + obj.real;

temp.image = image + obj.image;

cout<<"Addition of complex number is : ";

cout<<temp.real<<" + "<<temp.image<<"i"<<endl;

}

void Complex:: sub_Complex(Complex &obj){

Complex temp;

temp.real = real - obj.real;

temp.image = image - obj.image;

cout<<"Subtraction of complex number is : ";

cout<<temp.real<<" + "<<temp.image<<"i"<<endl;

}



CODE OUTPUT


Solution



Post a Comment

Previous Post Next Post