Write a program in C++ that defines a class named Student that represents a student. This
class declares three variables – name, stipend and course of suitable data types. It also has
a variable intern_status that is set to 1 if the student is interning in some organization and
0 otherwise. This variable is initialized appropriately in the constructors of the class. The
member functions of this class should be defined as given below:
ï‚· Parameterized constructor that initializes the values of all the members.
ï‚· An overloaded constructor that sets the stipend to zero for a non-interning student.
ï‚· Copy constructor for the class Student.
ï‚· Overload Operator << as a friend function in the class Student for displaying the
details of the student.
ï‚· Destructor for the class Student.
In the main() function, create two objects of this class; one for a ststuden
to create a new object and display it using << operator.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Student{
private:
string name;
string stipend;
string course;
int intern_status;
public:
//Parameterized constructor that initializes the values of all the members.
Student(string name,string stipend,string course,int intern_status){
this->name=name;
this->stipend=stipend;
this->course=course;
this->intern_status=intern_status;
}
//An overloaded constructor that sets the stipend to zero for a non-interning student.
Student(string name,string stipend,string course){
this->name=name;
this->stipend=stipend;
this->course=course;
this->intern_status=0;
}
//Copy constructor for the class Student.
Student(const Student &st) {
this->name=st.name;
this->stipend=st.stipend;
this->course=st.course;
this->intern_status=st.intern_status;
}
//Overload Operator << as a friend function in the class Student for displaying thedetails of the student.
friend ostream& operator<<(ostream&, const Student&);
//Destructor for the class Student.
~Student(){
}
};
inline ostream& operator<<(ostream& out, const Student& st){
out<<"Name: "<<st.name<<"\n";
out<<"Stipend: "<<st.stipend<<"\n";
out<<"Course: "<<st.course<<"\n";
if(st.intern_status==0){
out<<"A non-interning student.\n";
}else{
out<<"A interning student.\n";
}
return out;
}
int main(void){
//In the main() function, create two objects of this class; one for a ststudenÂ
//to create a new object and display it using << operator.
Student* st1=new Student("Peter","456456","C++",1);
Student* st2=new Student(*st1);
cout<<"Student1\n"<<*st1<<"\n";
cout<<"Student2\n"<<*st2<<"\n";
delete st1,st1;
system("pause");
return 0;
}
Comments
Leave a comment