intro to object oriented programming qn


The objective of this problem is to introduce a simple Object-Oriented Programming (OOP) problem and ensure that students understand the concept of OOP. In this case, it’s tested with the understanding of object.
Problem Description
Given a group of people with different heights and weights, determine the shortest and tallest people in the group, and calculate their body mass index(BMI).
The formula for BMI is
BMI = weight(in kg)/height(in meters)^2


The first line of the input contains an integer N (2 <= N <= 100) denoting the number of people in the group. The next N lines contain the information (name, height in centimeters, and weight in kilograms) of the people in the group.

Output the name of the shortest and tallest people in the group, assuming that there is only one shortest person and one tallest person in the group.

Suppose A is the shortest and B is the tallest person in the group, the output will be:

A is the shortest with BMI equals to C.
B is the tallest with BMI equals to D.
Output the BMI value correct to 2 decimal places.

Please refer to sample output for more details.
Sample Input

Diamond 178 55
Jarod 160 80
Douglas 180 60
Rod 151 48

Sample Output

Rod is the shortest with BMI equals to 21.05.
Douglas is the tallest with BMI equals to 18.52.

i can easily solve this using structures but have difficulty doing it using classes and objects. my attempt at a solution is below. im trying to create new student objects based on the number entered by the user using an array of pointers named "list". i tried creating an array of student objects "list2" and to initialise them i coded list2[i](name,height, weight); inside the for loop and both these methods didnt work out. appreciate all the help for this problem.

1 #include <iostream>
2 #include<string>
4 using namespace std;
6 class student{
8 private:
9 string name;
10 double height;
11 int weight;
13 public:
14 //constructor
15 student(string name1, double height1, int weight1)
16 {
17 name = name1;
18 height = height1;
19 weight = weight1;
20 }
22 //Member Methods
24 void displaystats()
25 {
26 cout << name << " " << height << " " << weight;
27 }
29 };
32 int main()
33 {
34 string name;
35 double height;
36 int n, i, weight;
37 cin >> n;
38 student* list[n];
41 for (i = 0; i < n; i++)
42 {
43 list = new student;
44 cin >> name;
45 cin >> height;
46 cin >> weight;
47 list[i](name, height, weight);
48 list++;
49 }
52 (*list[1]).displaystats;
55 return 0;
57 }

On Line 43 you're trying to use a constructor with no arguments without declaring or defining it. Then you try to save the result of that to 'list' but you don't tell the compiler where in 'list' you want it to be saved. Line 47 shows a lot of confusion so I'll make this quick suggestion: Comment out\delete Line 43, then replace Line 47 with this:
list[i] = new student(name, height, weight);

There's your free-bee for the day.

What are you trying to do at Lines 48 and 52?

By the way, it's a bit annoying to have to filter out the numbers from your post so try to avoid that in the future.
Topic archived. No new replies allowed.