Write a program to print the names of students by creating a Student class. If no name is passed while creating an object of Student class, then the name should be "Unknown", otherwise the name should be equal to the String value passed while creating object of Student class.
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
public:
Student(string name1="Unknown"):
name(name1) {}
void printName() {
cout << name << endl;
}
};
int main() {
Student test_student("test_name"); // in brackets passed name
Student unknown_test_student;
test_student.printName();
unknown_test_student.printName();
}
Comments
Leave a comment