1. Define a class named Family in a header file named: family.h This file acts a
ID: 3861314 • Letter: 1
Question
1. Define a class named Family in a header file named: family.h
This file acts as a prototype.
2. Create the implementation file family.cpp
a. The class will have the following members
1. Family name
2. Parent (array of 2)
a. First Name
b. Last Name
c. Age (you must validate the age is the proper data type.
3. Children (array of 10)
a. First Name
b. Last Name
c. Age (you must validate the age is the proper data type.
b. The class will be able to add parents or children
c. The class will be able to return information on parents and children.
3. Create the program Lab05b.cpp which uses the class Family to Implement the following
a. The program will allow the user to
1. Add families
a. With one or two parents
b. With zero or more children
2. Delete Families search by last name (should use the same search
function from item 3 below. (1 point)
3. Display Families searching by the last name
4. Display a list of all families
4. On exit print the following text to the screen; “So long from the <XXX> family!” where <XXX> is
the first family in the list (if the list is empty just print “Bye Bye”).
5. Bonus
a. Be able to store data in a file C: empamily.dat
when the program exits
b. Be able to read the data from the file C: empamily.dat
when the program starts.
Explanation / Answer
1)Examine the file named vehicle.h for simple class which we will use to begin our study of inheritance. It consists of four simple methods which can be used to manipulate data pertaining to our vehicle.
1. //class declaration part, vehicle.h, header file
2. //save and include this file in your project
3. //do not compile or run
4.
5. #ifndef VEHICLE_H //preprocessor directive
6. #define VEHICLE_H
7.
8. //----------class declaration part – the interface----------
9. class Cvehicle
10. {
11. protected: //new keyword
12. int wheels;
13. int weight;
14. public:
15. void initialize(int input_wheels, float input_weight);
16. int get_wheels(void);
17. float get_weight(void);
18. float wheel_load (void);
19. };
20. #endif
2)
Examine the file named vehicle.cpp, and you will find that it is the implementation of the vehicle class. The initialize() method assigns the values input as parameters to the wheels and weight variables.
1. //Program vehicle.cpp, implementation part,
2. //compile without error, generating object file, do not run
3. #include "vehicle.h"
4.
5. //---------------class implementation part---------------------
6. //initialize to data input by user
7. void Cvehicle::initialize(int input_wheels, float input_weight)
8. {
9. wheels = input_wheels;
10. weight = input_weight;
11. }
12.
13. //get the number of wheels of this vehicle
14. int Cvehicle::get_wheels()
15. {
16. return wheels;
17. }
18.
19. //return the weight of this vehicle
20. float Cvehicle::get_weight()
21. {
22. return weight;
23. }
24.
25. //return the load on each wheel
26. float Cvehicle::wheel_load()
27. {
28. return (weight/wheels);
29. }
3)
The file named transprt.cpp uses the Cvehicle class in similar manner as we have illustrated in the previous Module. This should be an indication to you that the Cvehicle class is just a normal class as defined in C++.
1. //program transprt.cpp, the main program,
2. //compile and run
3.
4. #include <iostream.h>
5. #include <stdlib.h>
6. #include "vehicle.h"
7. //user define header file and put it in the same folder as this program
8.
9. void main()
10. {
11. Cvehicle car, motorcycle, truck, sedan_car;
12. //4 objects instantiated
13.
14. //data initialization
15. car.initialize(4,3000.0);
16. truck.initialize(20,30000.0);
17. motorcycle.initialize(2,900.0);
18. sedan_car.initialize(4,3000.0);
19.
20. //Display the data
21. cout<<"The car has "<<car.get_wheels()<< " tires. ";
22. cout<<"Truck has load "<<truck.wheel_load()<<" kg per tire. ";
23. cout<<"Motorcycle weight is "<<motorcycle.get_weight()<<" kg. ";
24. cout<<"Weight of sedan car is "<<sedan_car.get_weight()<<" kg, and has
25. "<<sedan_car.get_wheels()<<" tires. ";
26.
27. system("pause");
28. }
4)
Examine the file named car.h, for our first example of using a derived class. The Cvehicle class is inherited due to the ":public Cvehicle" code added to line 12 as shown below:
class Ccar : public Cvehicle
1. //another class declaration car.h
2. //save and include this file in your project
www.tenouk.com
3. //do not compile or run.
4.
5. #ifndef CAR_H
6. #define CAR_H
7.
8. #include "vehicle.h"
9.
10. //-----------derived class declaration part------------------
11. //Ccar class derived from Cvehicle class
12. class Ccar : public Cvehicle
13. {
14. int passenger_load;
15. public:
16. //This method will be used instead of the same
17. //method in Cvehicle class - overriding
18. void initialize(int input_wheels, float input_weight, int people = 4);
19. int passengers(void);
20. }
;21.
22. #endif
5)
Examine the program named car.cpp which is the implementation file for the Ccar class.
- The first thing you should notice, this file has no indication of the fact that it is a derived class of any other class, and can only be determined by inspecting the preprocessor directive of the header file #include "car.h".
1. //Implementation file car.cpp for derived Ccar class
2. //compile without error and include in your project,
3. //Do not run
4.
www.tenouk.com
5. #include "car.h"
6.
7. //-------implementation part of the derived class car.h----------
8. void Ccar::initialize(int input_wheels, float input_weight, int people)
9. {
10. passenger_load = people;
11. wheels = input_wheels;
12. weight = input_weight;
13. }
14.
15. int Ccar::passengers(void)
16. {
17. return passenger_load;
18. }
6)
Examine the program named truck.h, an example of another derived class that uses the Cvehicle class as its based class. This class will specialize in those things that pertain to trucks.
1. //Another class declaration, truck.h,
2. //Save this file in your project
3. //Do not compile or run
4.
5. #ifndef TRUCK_H
6. #define TRUCK_H
7.
8. #include "vehicle.h"
9.
10. //----------class declaration part of derived class truck------------
11. class Ctruck : public Cvehicle
12. {
13. int passenger_load;
14. float payload;
15. public:
16. void init_truck(int how_many = 2, float max_load = 24000.0);
17. float efficiency(void);
18. int passengers(void);
19. };
20. #endif
7)
Examine the program named truck.cpp for the implementation of the Ctruck class.
- Compile this program without error, generating object file, in preparation for our program example that uses all the three classes Cvehicle, Ccar, Ctruck, defined in this Module.
1. //Derive class truck.cpp implementation
2. //Compile and include in your project
3. //Do not run
4.
5. #include "truck.h"
6.
7. //--------implementation part of the Ctruck derived class------
8. void Ctruck::init_truck(int how_many, float max_load)
9. {
10. passenger_load = how_many;
11. payload = max_load;
12. }
13.
14. float Ctruck::efficiency(void)
15. {
16. return payload/(payload + weight);
17. }
18.
19. int Ctruck::passengers(void)
20. {
21. return passenger_load;
22. }
8)
Examine the program named allvehicle.cpp carefully. This main program uses all three of the classes we have been discussing in this Module.
- Before compiling and running this program, make sure these files: vehicle.h, vehicle.cpp, car.h, car.cpp, truck.h, truck.cpp and allvehicle.cpp are in one project. Also, you have to compile without error the vehicle.cpp, car.cpp and truck.cpp files, generating object files. Compile and execute the allvehicle.cpp main program.
- Also make sure the first main() program transprt.cpp not in this project. We only need one main() program.
1. //The new main program for inheritance, allvehicle.cpp
2. //Compile and run this program
3.
4. #include <iostream.h>
5. #include <stdlib.h>
6. #include "vehicle.h" //the interface of the Cvehicle class
7. #include "car.h" //the interface of the Ccar class
8. #include "truck.h" //the interface of the Ctruck class
9.
10. void main()
11. {
12. Cvehicle unicycle; //base class
13.
14. cout<<"Unicycle using the Cvehicle base class ";
15. cout<<"-------------------------------------- ";
16.
17. unicycle.initialize(1,12.5);
18.
19. cout<<"Unicycle has "<<unicycle.get_wheels()<<" tire. ";
20. cout<<"Unicycle wheel load is "<<unicycle.wheel_load()<<"kg on one
21. tire. ";
22. cout<<"Unicycle weight is "<<unicycle.get_weight()<<" kg. ";
23.
24. Ccar sedan; //derived class
25.
26. cout<<"Sedan car using the Ccar derived class ";
27. cout<<"-------------------------------------- ";
28.
29. sedan.initialize(4,3500.0,5);
30.
31. cout<<"Sedan car carries "<<sedan.passengers()<<" passengers. ";
32. cout<<"Sedan car weight is "<<sedan.get_weight()<<" kg. ";
33. cout<<"Sedan car wheel load is "<<sedan.wheel_load()<<"kg per tire. ";
34.
35. Ctruck trailer; //derived class
36.
37. cout<<"Trailer using the Ctruck derived class ";
38. cout<<"-------------------------------------- ";
39.
40. trailer.initialize(18,12500.0);
41. trailer.init_truck(1,33675.0);
42.
43. cout<<"Trailer weight is "<<trailer.get_weight()<<" kg. ";
44. cout<<"Trailer efficiency is "<<100.00 * trailer.efficiency()<<"%. ";
45.
46. system("pause");
47. }
9)
Let start with a simple program skeleton.
//inheritance
#include <iostream.h>
#include <stdlib.h>
//base class
class Base
{
//member variables and member
//functions...
public:
Base(){}
~Base(){}
protected:
private:
}
;
//derived class...
class Derived:public Base
{
//same as normal class actually...
//member variables and member function...
public:
Derived(){}
www.tenouk.com
~Derived(){}
private:
protected:
}
;
void main()
{
cout<<"Testing the program skeleton..."<<endl;
system("pause");
}
9)
#include <iostream.h>
#include <stdlib.h>
//---class declaration and implementation------
//base class..
.class MyFather
{
//member variables and member
//functions...
private:
char* EyeColor;
char* HairType;
double FamSaving;
protected:
public:
MyFather(){}
~MyFather(){}
char* GetEye()
{ return EyeColor = "Brown";}
char* GetHair()
{ return HairType = "Straight";}
double GetSaving()
{return FamSaving = 30000;}
}
;
//derived class...
class MySelf:public MyFather
{
//same as normal class actually...
private:
char* MyEye;
char* MyHair;
public:
MySelf(){}
~MySelf(){}
char* GetMyEye()
{ return MyEye = "Blue";}
char* GetMyHair()
{return MyHair = "Curly";}
protected:
}
;
//another derived class...
class MySister:public MyFather
{
private:
char* SisEye;
char* SisHair;
public:
MySister(){}
~MySister(){}
www.tenouk.com
char* GetSisEye()
{return SisEye = "Black";}
char* GetSisHair()
{ return SisHair = "Blonde";}
}
;
//-----------main program---------------
int main()
{
//base class object...
MyFather Test1;
cout<<"Testing the inheritance program... "<<endl;
cout<<"My father's eye is = "<<Test1.GetEye()<<endl;
cout<<"My father's hair is = "<<Test1.GetHair()<<endl;
//derived class object...
MySelf Test2;
cout<<" My eye is = "<<Test2.GetMyEye()<<endl;
cout<<"My hair is = "<<Test2.GetMyHair()<<endl;
//the following are inherited from MyFather class...
cout<<"Our family saving is = $"<<Test2.GetSaving()<<endl;
cout<<"My father's eye is = "<<Test2.GetEye()<<endl;
cout<<"My father's hair is = "<<Test2.GetHair()<<endl;
//another derived class object...
MySister Test3;
cout<<" My sister's eye is = "<<Test3.GetSisEye()<<endl;
cout<<"My sister's hair is = "<<Test3.GetSisHair()<<endl;
//the following are inherited from MyFather class...
cout<<"Our family saving is = $"<<Test3.GetSaving()<<endl;
cout<<"My father's eye is = "<<Test3.GetEye()<<endl;
cout<<"My father's hair is = "<<Test3.GetHair()<<" ";
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.