Self-test 11:
C++ Classes
-
Which of the following two entities (reading from Left to Right) can be connected by the dot operator?
- A class member and a class object.
- A class object and a class.
- A class and a member of that class.
- A class object and a member of that class.
SolutionD
-
Which of the following can access private data members or member functions of a class?
- Any function in the program.
- All global functions in the program.
- Any member function of that class.
- Only public member functions of that class.
SolutionC
-
Which of the following access control is used in a class definition by default?
- Public
- Private
- Protected
- _It is not defined.
SolutionB
-
Which of the following is a valid class declaration?
- class A { int x; };
- class B { }
- public class A { }
- object A { int x; };
SolutionA
-
We want to implement the following class Int_List to store a list of integers.
#include
class Int_List { public: Int_List(); // constructor; initialize the list to be empty void append(int k); // add k to the end of the list private: int* item; // item will point to the dynamically allocated array int num_items; // number of items currently in the list }; Please implement the constructor to initialize the list to be empty.
SolutionInt_List::Int_List() { item = NULL; num_items = 0; }
-
Extend the Int_List class by adding a 2-argument constructor to allow an Int_List to be initialized to contain n copies of value v. You may use the append member function directly.
SolutionFirst, the following would be added to the public part of the class declaration:
Int_List(int n, int v);
Then, the following would be added to Int_List.cpp:Int_List::Int_List(int n, int v) { item = NULL; num_items = 0; for (int k = 0; k < n; k++) append(v); }
-
When Hollywood executives look at movies, they only care about two things: the name of the movie and the movie sales (in millions of dollars). Computer records at Hollywood studios maintain a Movie class. An example usage of the Movie class is shown below.
Movie m1 ("Lord of the Rings", 400); Movie m2 ("Star Wars", 200); if (m1 < m2) cout << m2.get_name() << " is the better movie because it made $" << m2.get_sales() << " million.\n"; else cout << m1.get_name() << " is the better movie because it made $" << m1.get_sales() << " million.\n";The declaration for the Movie class is given below, as it would appear in the movie.h file.
class Movie { public: Movie (char* n, double s); // Constructor char* get_name(void) const; // Look up name double get_sales(void) const; // Look up sales private: char name[30]; double sales; };
Write the function definitions for each of the 3 member functions in the Movie class, as they would appear in the movie.cpp file.
SolutionMovie::Movie (char* n, double s) { strcpy(name,n); sales = s; } char* Movie::get_name(void) const { return name; } double Movie::get_sales(void) const { return sales; }
-
Write a class Car that keeps track of the name of the car, the fuel efficiency in miles per gallon (mpg), the number of miles driven, and the number of gallons left in the gas tank. The constructor should take the car name and the fuel efficiency as input. Write the class declaration first and then define the member functions for the class. You'll have to figure out what functions to add based on the sample usage below.
Car my_car("Ferrari Testarossa", 20); // My Ferrari gets 20 miles per gallon my_car.add_gas(10); // Add 10 gallons of gas to the tank my_car.drive(100); // Drive 100 miles cout << my_car.get_name() << " has driven " << my_car.get_miles() << " miles and has " << my_car.get_gas() << " gallons left in the tank.";The output is: Ferrari Testarossa has driven 100 miles and has 5 gallons left in the tank.Solutionclass Car { public: Car(char* new_name, double new_mpg); void add_gas(double amount); void drive(double dist); char* get_name(void) const; double get_miles(void) const; double get_gas (void) const; private: char name[30]; double mpg; double miles; double gas; }; Car::Car(char* new_name, double new_mpg) { strcpy(name,new_name); mpg = new_mpg; miles = 0; gas = 0; } void Car::add_gas(double amount) { gas += amount; return; } void Car::drive(double dist) { miles += dist; gas -= dist/mpg; return; } char* Car::get_name(void) { return name; } double Car::get_miles(void) { return miles; } double Car::get_gas(void) { return gas; }
-
Write the declaration and definitions for a Point2D class that stores the x and y coordinates of a two-dimensional point. The constructor for the Point2D class should take the x and y coordinates as input. The distance_from function should calculate the distance between the given point and the current Point2D object. An example of code using the Point2D class is shown below. Your Point2D class should define all functions necessary to run the code below.
Point2D p1 (3, -4.2); Point2D p2 (10.2, 5.8); cout << "The coordinates of p1 are x=" << p1.get_x() << " and y=" << p1.get_y() << ".\n"; cout << "The distance between p1 and p2 is " << p1.distance_from(p2) << ".\n";Solutionclass Point2D { public: Point2D(double x0, double y0); double get_x(void) const; double get_y(void) const; double distance_from(Point2D p2) const; private: double x; double y; }; Point2D::Point2D(double x0, double y0) { x = x0; y = y0; } double Point2D::get_x(void) const { return x; } double Point2D::get_y(void) const { return y; } double Point2D::distance_from(Point2D p2) const { return sqrt( pow(x-p2.x,2) + pow(y-p2.y,2) ); }