Answer to Question #346471 in C++ for Omkar

Question #346471

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.

1
Expert's answer
2022-05-30T15:11:50-0400
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS