Create a class 'student'with three data member which are name, age and address. The constructor of the class assign default value to name as'unknow', age'0' and address as 'notavailable'. It has two functions with the same name 'setinfo'. First function has two parameter for name age and assigns the same where as the second function takes has three parameter which are assigned to name, age and address respectively. Print the same name age and address of 10 students. Hint- use array of object
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int age;
string address;
public:
Student(){
//default value to name as'unknow', age'0' and address as 'notavailable'.
this->name="unknow";
this->age=0;
this->address="notavailable";
}
void setinfo(string name,int age){
this->name=name;
this->age=age;
}
void setinfo(string name,int age,string address){
this->name=name;
this->age=age;
this->address=address;
}
void displayDetails(){
cout<<"The Student name is: "<<this->name<<"\n";
cout<<"The Student age is: "<<this->age<<"\n";
cout<<"The Student address is: "<<this->address<<"\n\n";
}
~Student(){
}
};
int main(){
string name;
int age;
string address;
Student students[10];
for(int i=0;i<10;i++){
cout<<"Enter Student name: ";
getline(cin,name);
cout<<"Enter Student age: ";
cin>>age;
cout<<"Enter Student address: ";
cin.ignore();
getline(cin,address);
students[i].setinfo(name,age,address);
}
for(int i=0;i<10;i++){
students[i].displayDetails();
}
system("pause");
return 0;
}
Comments
Leave a comment