Problem statement:
You are given a class called Student with four private data members: age, first_name, last_name, and standard. You need to implement the following member functions:
void set_age(int a): Sets the value of age to the given integer.
void set_first_name(string f): Sets the value of first_name to the given string.
void set_last_name(string l): Sets the value of last_name to the given string.
void set_standard(int s): Sets the value of standard to the given integer.
int get_age(): Returns the value of age.
string get_first_name(): Returns the value of first_name.
string get_last_name(): Returns the value of last_name.
int get_standard(): Returns the value of standard.
string to_string(): Returns a string in the format "age,first_name,last_name,standard".
Sample Input:
The input consists of five lines. The first line contains an integer representing the age, the second line contains a string representing the first name, the third line contains a string representing the last name, and the fourth line contains an integer representing the standard.
15
John
Doe
10
Sample Output:
Print the age, first name, last name, and standard of the student using the to_string() member function.
15,John,Doe,10
Explanation:
In this example, the input represents a student who is 15 years old, named John Doe, and is in the 10th standard. We create an instance of the Student class, set the age, first name, last name, and standard using the member functions, and then print the details using the to_string() member function.
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int age;
string first_name;
string last_name;
int standard;
public:
void set_age(int a) {
age = a;
}
void set_first_name(string f) {
first_name = f;
}
void set_last_name(string l) {
last_name = l;
}
void set_standard(int s) {
standard = s;
}
int get_age() {
return age;
}
string get_first_name() {
return first_name;
}
string get_last_name() {
return last_name;
}
int get_standard() {
return standard;
}
string to_string() {
return std::to_string(age) + "," + first_name + "," + last_name + "," + std::to_string(standard);
}
};
int main() {
int age, standard;
string first_name, last_name;
cin >> age;
cin.ignore(numeric_limits::max(), '\n');
getline(cin, first_name);
getline(cin, last_name);
cin >> standard;
Student st;
st.set_age(age);
st.set_first_name(first_name);
st.set_last_name(last_name);
st.set_standard(standard);
cout << st.to_string() << endl;
return 0;
}
Code Explanation
We define a class called Student with four private data members: age, first_name, last_name, and standard.
The class also has eight member functions:
set_age(int a): Sets the value of age to the given integer.
set_first_name(string f): Sets the value of first_name to the given string.
set_last_name(string l): Sets the value of last_name to the given string.
set_standard(int s): Sets the value of standard to the given integer.
get_age(): Returns the value of age.
get_first_name(): Returns the value of first_name.
get_last_name(): Returns the value of last_name.
get_standard(): Returns the value of standard.
to_string(): Returns a string in the format "age,first_name,last_name,standard".
Inside the main() function, we read the input values for age, first name, last name, and standard using cin and getline() functions.
Note: We use cin.ignore() to ignore the newline character after reading the integer age, so that it doesn't interfere with reading the first name.
We create an instance of the Student class called st.
We set the age, first name, last name, and standard of the student using the respective member functions of the st object.
Finally, we print the details of the student by calling the to_string() member function on the st object.
This solution demonstrates the usage of classes and objects in C++ to encapsulate data and behavior into a single entity.
I hope this solution and explanation meet your requirements and are helpful for understanding the problem and its solution!