In: Computer Science
C++
Classes & Objects
Create a class named Student that has three private member: string firstName string lastName int studentID
Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects.
In the 'main' function do the following (1) Create a student object "student1".
(2) Use set methods to assign
(3) Display the students detail using get functions in standard output using cout:
Sandy Santos 6337130
#include<iostream>
using namespace std;
class Student{
private:
string firstName;
string lastName;
int studentID;
public:
// Getter functions.
string getFirstName(){
return firstName;
}
string getLastName(){
return lastName;
}
int getStudentID(){
return studentID;
}
// Setter functions.
void setFirstName(string FirstName){
firstName = FirstName;
}
void setLastName(string LastName){
lastName = LastName;
}
void setStudentID(int StudentID){
studentID = StudentID;
}
};
int main(){
// Create Student object
Student student1;
// Use set functions to set fields.
student1.setFirstName("Sandy");
student1.setLastName("Santos");
student1.setStudentID(6337130);
// Using get methods to print.
cout<<student1.getFirstName()<<" "<<student1.getLastName()<<" "<<student1.getStudentID()<<endl;
return 0;
}
The output is -
The code has been commented so you can understand the code better.
I would love to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)
Happy Coding !!