Practical 1
Practical 1
Aim: Write a program for a simple class and object. Performing simple arithmetic operations using
C ++ class and object like, Addition, Subtraction, Multiplication & Division.
Compiler used: We used the g++ compiler to compile the C++ program.
Operating System used: Ubuntu
Theory:
The main purpose of C++ programming is to add object orientation to the C programming
language and classes are the central feature of C++ that supports object-oriented programming and
are often called user-defined types.
A class is used to specify the form of an object and it combines data representation and
methods for manipulating that data into one neat package. The data and functions within a class are
called members of the class.
A class definition starts with the keyword class followed by the class name; and the class
body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon
or a list of declarations. For example, we defined the Box data type using the keyword class as
follows −
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The keyword public determines the access attributes of the members of the class that
follows it. A public member can be accessed from outside the class anywhere within the scope of
the class object. You can also specify the members of a class as private or protected which we
will discuss in a sub-section.
Both of the objects Box1 and Box2 will have their own copy of data members.
Pseudo Code:
1. Start.
2. Declare the class Arithmetic
3. Declare the private and public data members and 5 member functions.
4. Define the member functions add(),sub(),mul(),div(),mod() in class and returning
statements.
5. In main function create an object of a class Arithmetic class
6. Access all member functions such as add(),sub(),mul(),div(),mod()by calling it using
object(“ .” operator)
7. Print results of all arithmetic operations.
8. Stop
Conclusion:
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
----------------
Sign of Teacher