0% found this document useful (0 votes)
7 views4 pages

Experiment No. 3

The document outlines an experiment focused on operations with complex numbers using C++. It covers the definition and implementation of a class for complex numbers, including methods for addition, subtraction, multiplication, division, and finding the complex conjugate. Additionally, it discusses class concepts, member functions, and provides an algorithm for program development along with oral questions for further understanding.

Uploaded by

gmranuj
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)
7 views4 pages

Experiment No. 3

The document outlines an experiment focused on operations with complex numbers using C++. It covers the definition and implementation of a class for complex numbers, including methods for addition, subtraction, multiplication, division, and finding the complex conjugate. Additionally, it discusses class concepts, member functions, and provides an algorithm for program development along with oral questions for further understanding.

Uploaded by

gmranuj
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/ 4

Experiment No: 02

Title: Operations on complex numbers: Add, Subtract, Multiply, Divide and Complex
conjugate

Aim:Write a program in C++ to perform following operations on complex numbers Add,


Subtract, Multiply, Divide, Complex conjugate. Design the class for complex number
representation and the operations to be performed

Objective:To learn the concepts classes and objects

Theory:

Classes in C++

A class is a mechanism for creating user-defined data types. It is similar to the C language
structure data type. In C, a structure is composed of a set of data members. In C++, a class
type is like a C structure, except that a class is composed of a set of data members and a set of
operations that can be performed on the class.

The class is a way to bind the data and its associated functions together. It allows the data
(and functions) to be hidden, if necessary, from the external use. When defining a class we
are creating a new abstract data type that can be treated like any other built-in data type.

Class declaration describes the type and scope of its members. The class definition describes
how the class functions are implemented. The general form of a class declaration is:

class class_name
{
private:
variables declarations;
functions declarations;

public:
variables declarations;
functions declarations;
};

The keyword class specifies that what follows is an abstract data of type class_name. The
functions and variables are collectively called class members. The variables declared inside
the class are known as data members and the functions are known as member functions. They
are usually grouped under two sections, namely, private and public.

Keywords private and public are known as visibility labels.

● Private: This access specifier makes sure that the private members can be accessed
from other members of the class only or from the friends of class.

● Public: This access specifier makes sure that the public members can be accessed from
anywhere through the object of the class.

Objects of Class

Instances or variables of classes are called objects. Once a class has been declared, we can
create variables of that type by using the class name like any other built-in type variables.

Syntax: class_name object_name;

The class definition provides only the template (like a structure in C) and does not create any
memory space for the object. The necessary memory space is allocated to an object when the
object of class type is created. Objects can also be created when a class is defined by placing
their names immediately after the closing brace as follows,

class class_name
{
private:
. . . . .;
. . . . .;
public:
. . . . .;
. . . . .;
}object1, object2, . . . , objectn;

Accessing Class Members

The private data of a class can be accessed only through the member functions of that class.
Syntax:

object_name.function_name(actual_arguments);

Defining Member Functions

a. Outside the class definition: Member functions that are declared inside the class have to
be defined separately outside the class. The member function incorporates a
membership ‘identity label’ in the function header. This ‘label’ tells compiler which
class the function belongs to.

Syntax:

return_type class_name::function_name(arguments
declaration)
{
function_body;
}
The membership label class_name:: tells compiler that function_name belongs
to the class class_name. That is the scope of the function is restricted to the
class_name specified in the header line. ::is the scope resolution operator.

b. Inside the class definition:Another method of defining member function is to replace


the function declaration by the actual function definition inside the class. When a
function is defined inside the class, it is treated as an inline function. Normally only
small functions are defined inside the class definition.

Program Algorithm:

Design the class for complex number representation and the operations to be performed.

1. Include required header files.

2. Define the class complex with data members real andimg, and member functions
read,(), display(), add(), subtract(), multiply(), divide() and conjugate(),

3. Define the member functions outside the class.e.g. Function to add Complex Number
is:

void complex :: add(complex c1)


{
complex sum;
sum.real = real+c1.real;
sum.img = img+c1.img;
cout<<"Sum is : "; sum.display();
}
4. Define the main() function. Create two objects c1 and c2 of complex type. Invoke
the different member functions to perform operations on complex number through
object c1 and c2, using dot operator.

Conclusion:

___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
____________

Oral Questions:

1. What is a class? How it is differ from structure in C.

2. What are the objects? How they are created?

3. How does class accomplish data hiding?

4. Describe the mechanism of accessing data members and member function functions in the
cases:

a. Inside the main() program.

b. Inside a member function of same class.

c. Inside a member function of another class.

5. What is friend function? What are the merits and demerits of using friend functions?

6. Can we use same function name for a member function of a class and an outside function in
the same program file? If yes, how are they are distinguished? If no, give reason.

7. Define a class to represent a bank account. Include the following;

a. Data members: Name of the depositor, Account Number, Type of Account, Balance
amount in the account

b. Member Functions: To Assign Initial values, To deposit an amount, To withdraw an


amount after checking the balance, To display data.

You might also like