Experiment No. 3
Experiment No. 3
Title: Operations on complex numbers: Add, Subtract, Multiply, Divide and Complex
conjugate
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.
● 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.
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;
The private data of a class can be accessed only through the member functions of that class.
Syntax:
object_name.function_name(actual_arguments);
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.
Program Algorithm:
Design the class for complex number representation and the operations to be performed.
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:
Conclusion:
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
____________
Oral Questions:
4. Describe the mechanism of accessing data members and member function functions in the
cases:
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.
a. Data members: Name of the depositor, Account Number, Type of Account, Balance
amount in the account