Lab 4
Lab 4
LAB#4: CLASSES AND CONSTRUCTORS
This lab session introduces a brief idea of solving different problems using classes and constructors.
Students are required to complete the programming during the lab and get it checked by the instructor.
Besides, the students should also complete the lab assignments, given at the end of this manual, during
the lab. Late lab submissions will be graded accordingly.
NOTES:
1. Indentation of the implemented code will be strictly checked.
2. Students must make sure that they do not copy the code, assigned to them, from their
NOTES
fellow students. Any student caught in this regard will not be pardoned.
3. Each student should make a folder carrying his/her registration number in their Z drive. This
main folder will carry week wise sub‐folders. Everything done during one lab session should
be placed in each of those sub‐folders.
4. Students are requested to bring their lab manuals with them before coming to the lab.
USE OF CLASSES___ _____________
MAKING A CLASS:
class vehicle
{
public:
int doors;
int seats;
};
This creates a class with the name “vehicle”. This class has two variables that define the data for
vehicles. Now this class is included in the program and objects are made from this class as follows:
/* prog1.c */
/* making a class */
#include <stdio.h>
class vehicle
{
public:
int doors;
int seats;
};
int main()
{
vehicle taxi; // object instantiation
All students are required to instantiate another object of class vehicle with any other vehicle name and
define data for that vehicle. Also print the data corresponding to that vehicle.
Now modify the above program so that the program takes the input from the user to declare the
number of doors and seats for the two vehicles defined in the program above.
MAKING FUNCTIONS FOR CLASSES:
/* prog2.c */
/* making a class */
#include <stdio.h>
class vehicle
{
public:
int doors;
int seats;
int fuel;
int miles_per_litre;
};
int main()
{
vehicle taxi; // object instantiation
cout<<”range for taxi is: “<<taxi.range()<<endl;
return 0;
}
The range returned by the function can also be stored in some other variable like the following:
int main()
{
vehicle taxi; // object instantiation
int range;
return 0;
}
Modify the above program in which two vehicle objects are created. Then:
• Enter the values of fuel and miles_per_litre for the two vehicle objects from the keyboard.
• Use the vehicle objects to call the same member function i.e. range(); and then calculate the
values for the range of the two vehicles.
• In the main method, display the range returned by the two function calls.
PASSING PARAMETERS TO THE MEMBER FUNCTIONS:
/* prog3.c */
/* making a class */
#include <stdio.h>
class vehicle
{
public:
int doors;
int seats;
int fuel;
int miles_per_litre;
return fuel*miles_per_litre;
}
int main()
{
vehicle taxi; // object instantiation
Modify the above program to create two objects for vehicle class. Then:
• Declare two separate integer type variables in the main method. Name them “f” and “mpl”.
• Now take user input for “f” and “mpl”.
• Call the range function and pass the input values to the function parameters.
• Calculate range for both vehicle objects and display the range in the main method.
PUBLIC AND PRIVATE SPECIFIERS:
Consider the following code segment to understand the difference and use of private and public
specifiers:
/* prog4.c */
/* making a class */
#include<iostream.h>
class ratio
{
private:
int num;
int den;
int number;
public:
int display_ratio(int n, int d);
};
int main()
{
ratio r1;
cout<<r1.display_ratio(4,2)<<endl;
return 0;
}
The class “ratio” can also be written as follows:
class ratio
{
private:
int num;
int den;
int number;
public:
int display_ratio(int n, int d)
{
num = n;
den = d;
number = n/d;
return number;
}
};
Notice that if the member function is defined inside the class, class name is not used in the header. Now
modify the above program such that:
• Enter the values for the numerator and denominator from the keyboard, declaring the variables
as private.
• Define another member function for class “ratio” to print the ratio.
• In the main method, call the printing function using the object of class “ratio”.
USE OF CONSTRUCTOR __________
Constructors are member functions with the same name as the class. A constructor is a special function
that initializes the data members of a class. A class constructor function is called whenever an object of
that class is created. Several constructors can be defined for one class. Consider the following code to
understand the use of constructors.
/* prog5.c */
/* using constructors */
#include<iostream.h>
class ratio
{
private:
int num;
int den;
int number;
public:
ratio()
{
num = 0;
den = 1;
}
void print()
{
cout<<”ratio is:”<<num/den<<endl;
}
};
int main()
{
ratio r1; // the constructor automatically assigns the values of 0
// and 1 to num and den, respectively
return 0;
}
Now consider the following program which has 2 constructors(of course of the same name as the class).
/* prog6.c */
/* using constructors */
#include<iostream.h>
class ratio
{
private:
int num;
int den;
int number;
public:
ratio()
{
num = 0;
den = 1;
}
ratio(int n, int d)
{
num = n;
den = d;
}
void print()
{
cout<<”ratio is:”<<num/den<<endl;
}
};
int main()
{
ratio r1, r2(2,7);
return 0;
}
Similarly, any number of constructers can be defined having the same name but different parameter list.
LAB ASSIGNMENTS _____________
1. Make a class with the name “number”. The class should have the following member functions:
• To declare a several numbers by values entered from the user.
• To add two user defined numbers. Display the result.
• To multiply two user defined numbers. Display the result.
• To subtract two user defined numbers. Take special care when subtracting larger
number from smaller number. Define separate conditions inside the member function in
such a case. Append “‐” sign with the number if the result of subtraction is negative.
Call these functions in the main method using objects of class “number”.
2. Modify the above program to deal with complex numbers. Now the user will have to enter the
real and imaginary parts of the complex numbers separately. Remember that the arithmetic
operations on complex numbers is carried out as follows:
Addition: (a + ib) + (x + iy) = (a + x) + i(b + y);
Subtraction: (a + ib) - (x + iy) = (a - x) + i(b - y);
Multiplication: (a + ib) * (x + iy) = a(x + iy) + ib(x + iy) = ax – by + i(ay + bx);
Where a, b, x, y are real numbers and “i” stands for “iota”.
Again call these functions in the main method for different objects of class “complex”.
HOMEWORK __________________
1. What is a destructor? How is a destructor defined in a class?
2. Make a class “rational”. The user should enter numerator and denominator of the rational
number separately. He/she should not enter the rational number. The member functions of the
class should do the following:
a. Displays the rational number such that the number is printed in its lowest possible form.
For example if the user enters 4 as denominator and 16 as numerator the member
function prints 4 as output. If numerator is 7 and denominator are 3 the member
function prints 7/3. Similarly if both the denominator and numerator are negative the
rational number displayed should be positive. Implement separate conditions inside the
function to check for these.
b. Cubes the input rational number and displays the output.
c. Multiply two rational numbers and displays the output.
d. Adds two rational numbers and displays the result.
e. Subtracts two rational numbers and displays the result. Take care while subtracting
larger fraction from smaller.