This page is optimized for mobile devices, if you would prefer the desktop version just click here

1.3 Labworks  (Page 7/11)

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.

2.2) 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.

2.3) 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:

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.