Write a program that creates a class called student. The data members of the class are name and age.
·      Create a nullary constructor and initialize the class object.
·      Create a parameterized constructor that can set the values being passed from the main function.
·      Create a display function called showall( ) which will be used to show values that have been set.
Use the default copy constructor to show that copying of simple objects can be accomplished through the use of the default copy constructor.
#include <iostream>
#include <string>
using namespace std;
class Student
{
string name;
int age;
public:
Student() {}
Student(string _name, int _age):name(_name),age(_age) {}
void Assign()
{
cout << "Please, enter a name of student: ";
cin >> name;
cout << "Please, enter an age of student: ";
cin >> age;
}
void Showall()
{
cout << "The name of student is " << name
<< "\nThe age of student is " << age<<endl;
}
};
int main()
{
Student a("Jack", 18);
cout << "Info about student a(parametrized constructor):\n";
a.Showall();
Student b;
b.Assign();
cout << "Info about student b(nullary constructor):\n";
b.Showall();
Student c(b);
cout << "Info about student c(default copy constructor):\n";
c.Showall();
}
Comments
Leave a comment