C++ Lab 15 Alpha

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

LAB MANUAL # 15

Course: Computer Programming (CP-107)


Session: 22CP (alpha)

Classes

This lab has been designed to enable students to understand the concept of object-oriented
programming and how to use classes for efficient programming in C++. Clear concept about
classes has already been provided in the class along with examples.

Objectives:
In this lab, you will practice:
• Creating class.
• Creating class objects.
• Accessing class members using dot operator.
• Using scope resolution operator.
• Creating constructors.
• Inline functions

Lab Tasks: 15/


Question # 1: 2/
Write a class declaration named Circle with a private member variable named radius. Write
set and get functions to access the radius variable, and a function named getArea that returns
the area of the circle. (The area is calculated as 3.14159 * radius * radius)
Question # 2: 2/
Write a class named Car that has the following member variables:
• yearModel. An int that holds the car’s year model.
• make. A string that holds the make of the car.
• speed. An int that holds the car’s current speed.
In addition, the class should have the following constructor and other member functions.
• Constructor. The constructor should accept the car’s year model and make as
arguments.
• accelerate. The accelerate function should add 5 to the speed member variable each
time it is called.
• brake. The brake function should subtract 5 from the speed member variable each
time it is called.
Demonstrate the class in a program that creates a Car object, and then calls the accelerate
function five times. After each call to the accelerate function, get the current speed of the car
and display it. Then, call the brake function five times. After each call to the brake function,
get the current speed of the car and display it.
Question # 3: 3/
LAB MANUAL # 15
Course: Computer Programming (CP-107)
Session: 22CP (alpha)

Consider a class Movie that contains information about a movie. The class has the following
attributes:
• The movie name.
• The number of people that have rated this movie as a 1 (Terrible)
• The number of people that have rated this movie as a 2 (Bad)
• The number of people that have rated this movie as a 3 (OK)
• The number of people that have rated this movie as a 4 (Good)
• The number of people that have rated this movie as a 5 (Great)
Write a function addRating that takes an integer as an input parameter. The function should
verify that the parameter is a number between 1 and 5, and if so, increment the number of
people rating the movie that match the input parameter. For example, if 3 is the input
parameter, then the number of people that rated the movie as a 3 should be incremented by
1. Write another function, getAverage, that returns the average value for all of the movie
ratings. Finally, add a constructor that allows the programmer to create the object with a
specified movie name.
.
Question # 4: 2/
Define a class called CounterType. An object of this type is used to count things, so it records
a count that is a nonnegative whole number. Include a default constructor that sets the
counter to zero and a constructor with one argument that sets the counter to the value
specified by its argument. Include member functions to increase the count by 1 and to
decrease the count by 1. Be sure that no member function allows the value of the counter to
become negative. Also, include a member function that returns the current count value and
one that outputs the count to a stream.

Question # 5: 2/
Write a grading program for a class with the following grading policies:
• There are two quizzes, each graded based on 10 points.
• There is one midterm exam and one final exam, each graded based on 100 points.
• The final exam counts for 50 percent of the grade, the midterm counts for 25 percent,
and the two quizzes together count for a total of 25 percent.

Any grade of 90 or more is an A, any grade of 80 or more (but less than 90) is a B, any grade
of 70 or more (but less than 80) is a C, any grade of 60 or more (but less than 70) is a D, and
any grade below 60 is an F. Create a class studentRecord. The student record class should
have member variables for all the input data described above and a member variable for the
student’s weighted average numeric score for the entire course as well as a member variable
for the student’s final letter grade. Make all member variables private.
Include member functions for each of the following:
• member functions to set each of the member variables to values given as an
argument(s)
• to the function.
LAB MANUAL # 15
Course: Computer Programming (CP-107)
Session: 22CP (alpha)

• member functions to retrieve the data from each of the member variables.
• a void function that calculates the student’s weighted average numeric score for the
entire course and sets the corresponding member variable.
• a void function that calculates the student’s final letter grade and sets the
corresponding member variable.

Question # 6: 2/
Create a class book that has three data members, book title, book number, book author.
Create a default constructor and destructor for object initialization and destruction
respectively. Only member function of the class is Find(). Define this function outside class
and make it inline. Function takes book type object as argument and returns true if book title
has word “computer” in it, otherwise returns false.

Question # 7: 2/
Observe output of following code:
#include <iostream>
using namespace std;
class Tank
{
private:
int gallons;
public:
Tank()
{ gallons = 50; }
Tank(int gal)
{ gallons = gal; }
int getGallons()
{ return gallons; }
};
int main()
{
Tank storage[3] = { 10, 20 };
for (int index = 0; index < 3; index++)
cout << storage[index].getGallons() << endl;
return 0;
}

****************

You might also like