Question 3: You are given a class below. class Student{ public : string name ; i
ID: 666100 • Letter: Q
Question
Question 3: You are given a class below.
class Student{
public:
string name;
int age;
void introduce()
{
cout << “Hello, my name is ” << name << “, and I am ” << age << “years old” << endl;
}
};
Create a constructor to initialize both name and age;
Answer:
Create an object whose name and age are “Li Wang”, 33, respectively, and use pointer points to that object.
Answer:
Change the age of the object to 34 by using pointer.
Answer:
Call the function introduce() by using pointer.
Answer:
Question 4: Separate the Student class into two files: header file and implementation file.
Answer:
Question 5: Create a Professor class, and add any two properties and two functions to the class. In addition, you need to create a constructor to initialize all two properties. No objects are needed.
Explanation / Answer
Question :- Create a constructor to initialize both name and age;
Answer :-
Student(string n,int a){
name = n;
age = a;
}
Question :- Create an object whose name and age are “Li Wang”, 33, respectively, and use pointer points to that object
Answer :-
Student* st = new Student("Li Wang",33);
Question :- Change the age of the object to 34 by using pointer.
Answer :-
st->age = 34;
Question :- Call the function introduce() by using pointer.
Answer :-
st-introduce();
Question 4.
Student.h
class Student{
public:
string name;
int age;
Student(string n,int a){
name = n;
age = a;
}
void introduce(){
cout << “Hello, my name is ” << name << “, and I am ” << age << “years old” << endl;
}
};
Student.cpp
int main(){
Student *st = new Student("Li Wang",33);
st->age = 34;
st-introduce();
}
Question 5.
class Professor{
public:
string name;
int age;
Professor(string n,int a){
name = n;
age = a;
}
int get_age(){
return age;
}
string get_name(){
return name;
}
void set_name(string n){
name = n;
}
void set_age(int n){
age = n;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.