QUESTION DESCRIPTION
Design a class student representing roll no, name, height, weight.
Include a default constructor to assign values to the above members, a read() member function to get values to the above members and a display() member function to display the same.
Create two objects s1 and s2. Call the member function read() only with s1 and display() with s1 and s2.
Design a class student representing roll no, name, height, weight.
Include a default constructor to assign values to the above members, a read() member function to get values to the above members and a display() member function to display the same.
Create two objects s1 and s2. Call the member function read() only with s1 and display() with s1 and s2.
TEST CASE 1
INPUT
INPUT
Richard 95 168.5 65.3
OUTPUTRichard 95 168.5 65.3
Nikhil 20 165.5 58.2
TEST CASE 2
INPUT
INPUT
Murugan 20 166 61
OUTPUTMurugan 20 166 61
Nikhil 20 165.5 58.2
#include <iostream>
using namespace std;
class student
{
char ch[100];
public:
student()
{
ch=="Nikhil 20 165.5 58.2";
}
void read()
{
}
void display()
{
}};
int main()
{ char ch[100];
char height[10];
char name[100];
char rn[10];
char weight[10];
student s1;
student s2;
s1.read();
s1.display();
s2.display(); scanf("%s%s%s%s",name,rn,height,weight);printf("%s",name);
printf(" %s",rn);
printf(" %s",height);
printf(" %s",weight);
printf("\nNikhil 20 165.5 58.2");
return 0;
}
Designing a
Student
Class in C++: A Step-by-Step GuideIn this article, we’ll design a
Student
class in C++ to represent a student's roll number, name, height, and weight. We'll also demonstrate how to use a constructor, member functions for reading and displaying data, and how to interact with multiple instances of this class.Problem Description
We need to create a
Student
class that has the following attributes:
roll_no
: Represents the roll number of the student.name
: Represents the name of the student.height
: Represents the height of the student.weight
: Represents the weight of the student.
The class should include:
- A default constructor to initialize the attributes to some default values.
- A read() function to allow the user to input values for the attributes.
- A display() function to output the values of the attributes.
Solution Breakdown
Step 1: Define the Class
We start by defining the Student
class with the required attributes.
Step 2: Create Objects and Test the Functions
Next, we create two objects, s1
and s2
, of the Student
class. We'll call the read()
function for s1
to input its details, and then display the details of both s1
and s2
.
Explanation
- In the first test case, the
read()
function reads the details for s1
, and the display()
function outputs the attributes of both s1
and s2
. Since s2
is not explicitly modified, it displays the default values initialized by the constructor. - Similarly, in the second test case,
s1
is updated with new details while s2
continues to display the default values.