CS304 Object Oriented Programming Assignment 1 Solution Fall 2014

Assignment
Problem Statement:
Write the C++ language program that should fulfill the following requirements:
Program should contain two classes as “Student” and “Instructor”. Within main() function, create the objects of these two classes. Display your student ID and name by passing as attributes(student ID, name) while instantiating the object of the “Student” class. Using the object of the Instructor Class, call its member function Input() to take input for the height of the 8 instructors then call its member function print() to display the average height of the instructors.
Declare data members by private and member functions by public access specifier for both classes.
Function
Description
main() function
Objects of both classes will be created in it, and respective member functions of these objects will be called.
Student(char *, char *)
Constructor of the Student class to display your own student ID and name
input()
Function of Instructor class , it will take heights as input of 8 instructors
print()
Function of Instructor class, it will display average height of 8 instructors.

Detailed Description:
Your program should display your own student ID and name on the top of the screen through passing as attributes at the time of instantiating object of the Student class i.e.
Student ID : MC104403787
Student Name : Rehman
Member function input() of the “Instructor” class should take height as input of the 8 instructors from the user i.e.
Enter Instructor 1 Height in Feet : 5.5
Member function print() of the “Instructor” class should display average height of the Instructors.
Sample output of the program:
Solution:

#include <iostream>
#include <cstdlib>
#include <string.h>
#include <stdlib.h>

using namespace std;
//starting the student class
class Student{
private:
char * StudentID;
char * StudentName;
public:
Student(char *, char *);
void print(); //parametarized constructor of student class
~Student(); //destructor of student class

};

Student::Student(char *id, char *name)
{
StudentID = id;
StudentName = name;
}

void Student::print()
{
cout”Student ID : ” StudentID “endl”;
cout “Student Name : ” StudentName “\n\n”;
}

Student::~Student()
{

}

class Instructor{
private:
float age[8];

public:
Instructor(){ }
~Instructor(){ }
void input();
void print();
};

void Instructor::input()
{
int i;
for(i=1; i<=8; i++)
{
cout “Please enter the age of ” i ” Instructor in Feet : “;
cin >> age[i];
cout”\n\n”;
}
}

void Instructor::print()
{
int i;
float totalage = 0, averageage = 0;
for(i=1; i<=8; i++)
{
totalage+=age[i];
}
averageage = totalage/8;
cout “Average Height in Feet : ” “averageage”;
}

main()
{
Student student1((char *)”BC130403023″, (char *)”Aleena”);
student1.print();

Instructor heights;
heights.input();
heights.print();

cout “\n\n”;
system(“pause”);
}

DOWNLOAD SOLUTION HERE
loading...