Lab Sheet 6
Lab Sheet 6
Understanding the Concept of Type Conversion and Inheritance
To convert data from basic type to user-defined type and vice versa we cannot rely on built-in conversion routines because the compiler doesn't know anything about user-defined types. We have to provide the conversion routines to be used for typecasting. Following methods are used for conversion.
a) To convert from basic type to user-defined type, conversion is done by using the constructor function with one argument of basic type as follows
b) To convert from user-defined type to basic type, conversion is done by overloading the cast operator of basic type as a member function as follows
The conversions between objects of different classes can be carried out by using similar methods for conversions between basic types and user-defined types.
Inheritance is the concept by which the properties of one entity are made available to another. It allows new classes to be built from older and less specialized classes instead of being rewritten from scratch. The class that inherits properties and functions is called the subclass or the derived class and the class from which they are inherited is called the superclass or the base class. The derived class inherits all the properties of the base class and can add properties and refinements of its own. The base class remains unchanged.
In the above example, person is the base class whereas student is the derived class which inherits all the features of the base class. Multiple classes may be derived from the same base class and a derived class can also inherit characteristics of two or more classes.
A special pointer called this pointer can be employed in C++ programs. When the object needs to point to itself then this pointer is used. Remember this pointer points (or represents the address of the) object of the class but not the class.
Write a program that can convert the Distance (meter, centimeter) to meters measurement in float and vice versa. Make a class distance with two data members, meter and centimeter. You can add function members as per your requirement.
Write two classes to store distances in meter-centimeter and feet-inch systems respectively. Write conversions functions so that the program can convert objects of both types.
Create a class called Musicians to contain three methods string(), wind(), and perc(). Each of these methods should initialize a string array to contain the following instruments
Write three derived classes inheriting functionality of base class person (should have a member function that asks to enter name and age) and with added unique features of student, and employee, and functionality to assign, change and delete records of student and employee. And make one member function for printing the address of the objects of classes (base and derived) using this pointer. Create two objects of the base class and derived classes each and print the addresses of individual objects. Using a calculator, calculate the address space occupied by each object and verify this with address spaces printed by the program.
Write a base class that asks the user to enter a complex number and make a derived class that adds the complex number of its own with the base. Finally, make a third class that is a friend of derived and calculate the difference of the base complex number and its own complex number.