You are here: Home » Content » Introduction to Classes

Introduction to Classes

Module by: Dr. Duong Tuan Anh

1. OBJECTIVE

The objectives of Lab session 8 are (1) to get familiar with how to define object classes; (2) to practice to write constructors and (3) to learn how to dynamically allocate/deallocate memory on the heap.

2. EXPERIMENT

  • a. Read to understand the following program which uses the class student. Organize the program in one source program and run it on a C++ environment.
#include<iostream.h>
class student{
private:
long int rollno;
int age;
char sex;
float height;
float weight;
public:
void getinfo();
void disinfo();
};
void student::getinfo()
{
cout << " Roll no :";
cin >> rollno;
cout << " Age :";
cin >> age;
cout << " Sex:";
cin >> sex;
cout << " Height :";
cin >> height;
cout << " Weight :";
cin >> weight;
}
void student::disinfo()
{
cout<<endl;
cout<< " Roll no = "<< rollno << endl;
cout<< " Age =" << age << endl;
cout<< " Sex =" << sex << endl;
cout<< " Height =" << height << endl;
cout<< " Weight =" << weight << endl;
}
void main()
{
student a;
cout << " Enter the following information " << endl;
a.getinfo();
cout << " \n Contents of class "<< endl;
a.disinfo();
}
b. Reorganize the program into an interface file and an implementation file and then run the program.
  • Given the class student as defined in 2.1.a. Write a complete C++ program in which the main() function creates an array of size 10 to store student objects and prompts the user to enter the data for the student objects in the array and then displays the objects in the array.
  • Given the class student as defined in 2.1.a. Write a complete C++ program in which the main() function performs the following tasks:
  • to declare a run-time-allocated array on the heap to contain the student objects.
  • to prompt the user to enter an integer n and allocate a memory area on the heap to store n student objects.
  • to prompt the user to enter the student objects and store them in the array and display all the objects on the screen.
  • to deallocate the memory area for the array on the heap.
2.4 Test the following program:
class Int{
private:
int idata;
public:
Int(){
idata=0;
cout<<"default constructor is called"<<endl;
}
Int(int d){
idata=d;
cout<<"constructor with argument is called"<<endl;
}
void showData(){
cout<<"value of idata: "<<idata<<endl;
}
};
void main()
{
Int i;
Int j(8);
Int k=10;
Int *ptrInt = new Int();
ptrInt->showData();
delete ptrInt;
}
What are the outputs of the program? Explain the results.
What is the purpose of the statement delete ptrInt; in the program?
2.5 Define a class named Rectangle which contains two single-precision floating point data members: length and width. The class has some member functions:
  • A constructor with no parameters that assigns 0 to two data members of the created object.
  • A constructor with two single-precision floating-point parameters which assigns two parameters to the two data members of the created object.
  • Function perimeter() to compute the perimeter of the rectangle.
  • Function area() to compute the area of the rectangle.
  • Function getdata( ) to prompt the user to enter the length and width for a rectangle.
  • Function showdata( ) to display length, width, perimeter and area of a rectangle.
Include the class Rectangle in a complete C++ program. The main() function of this program creates two Rectangle objects using the two constructors respectively and displays the data of the two objects to check the working of all the member functions.
Then modify the program by replacing the two above constructor functions by a constructor with default arguments.
2.6 Define a class named CStudent which consists of the following data members:
  • Student id-number (integer).
  • An array of size 5 to contains at most 5 grades (single-precision floating point numbers).
  • An integer to indicate the number of entered grades.
The class also has the following member functions:
  • the constructor which assigns the initial values 0 to all data members of each Cstudent objects.
  • A function to get a student-id-number.
  • A function to get one grade and update the total of the entered grades.
  • A function to compute the average of all entered grades of a student.
  • A function to display student-id-number, and the average grade of that student.Include the class CStudent in a complete C++ program. This program creates one CStudent object, inputs the data for the object and displays the object’s data to verify the workings of the member functions.
2.6 Test the following program which uses a run-time allocated array.
#include <iostream.h>
void main()
{
int num;
cout<< “Please enter the numbers of input: ”
cin>>num;
int a = new int [num];
int total = 0; // Holds total of user’s eight numbers.
int ctr;
for (ctr=0; ctr<num; ctr++)
{
cout << “Please enter the next number...”;
cin >> a[ctr];
total += a[ctr];
}
cout << “The total of the numbers is “ << total << “\n”;
return;
delete [] a;
}

2.7 Given a class named IntArray that contains two private data members: a pointer to the beginning of the array, and an integer representing the size of the array. The public functions include a constructor and member functions that show every element in the IntArray, and show the first IntArray element only. The definition of the class IntArray is as follows:

// IntArray.h
class IntArray
{
private:
int* data; //pointer to the integer array
int size;
public:
IntArray(int* d, int s);
void showList();
void showFirst( );
};
// IntArray.cpp
IntArray::IntArray(int* d, int s)
{
data = d;
size = s;
}
void IntArray::showList()
{
cout<<"Entire list:" <<endl;
for(int x = 0; x< size; x++)
cout<< data[x]<<endl;
cout<< "----------------------"<< endl;
}
void IntArray::showFirst()
{
cout<< "First element is ";
cout << data[0]<< endl;
}
a. Add to the class IntArray one more member function named findMax which returns the largest element in the array.
b. Write a main program that instantiates one array of integers and then displays the array, the first element and the largest element of the array.

Comments, questions, feedback, criticisms?

Send feedback