/*************************
+---------------------------+
| Question 8 ( 2.0 marks ) |
+---------------------------+
Class "Student" inherits from class "Person". "Student" class
has a const attribute called "id". Complete the "Student"
class to produce the following output:
Name = Michael
ID = 111
Name = Kelly
ID = 222
*********************************************************/
#include <iostream>
usingnamespace std;
class Person {
string name;
public:
Person (string name)
: name(name) {}
void print() const {
cout << "Name = " << name << endl;
}
};
class Student : public Person {
constint id;
public:
Student (string name, int id)
/////// Write your code below ////////////////////////////
: Person(name),id(id)
{};
void print() const {
cout << name << endl;
cout << "ID = " << id << endl;
}
/////// End writing your coding //////////////////////////
};
int main() {
Student s1("Michael", 111);
Student s2("Kelly", 222);
s1.print();
s2.print();
return 0;
}
int this programe, i'm trying to access the private member name ((class Person)) and i can't modify the class person becuase it's not within // Write your code below , i think in this case i have to create a new method but i'm not sure.
/**************
+---------------------------+
| Question 10 ( 2.0 marks ) |
+---------------------------+
Correct the "Staff" class to produce the following output:
I'm a staff. My id is Steve
I'm a manager. My id is Maggie. My room no. is 1234
*********************************************************/
#include <iostream>
usingnamespace std;
class Staff {
public:
Staff (string id = "") {
this->id = id;
}
/////// Write your code below ////////////////////////////
void print() {
cout << "I'm a staff. My id is " << id << endl;
}
private:
string id;
/////// End writing your coding //////////////////////////
};
class Manager : public Staff {
int roomNumber;
public:
Manager (string id, int roomNumber) {
this->id = id;
this->roomNumber = roomNumber;
}
void print() {
cout << "I'm a manager. My id is " << id
<< ". My room no. is " << roomNumber << endl;
}
};
int main() {
Staff s("Steve");
Manager m("Maggie", 1234);
Staff* p;
p = &s;
p->print();
p = &m;
p->print();
return 0;
}
here, when i change the private to be protected, then i display only the staff class, how to solve this?
/////// Write your code below ////////////////////////////
template <typename T>
void add(T x, T y)
{
x + y;
}
/////// End writing your coding //////////////////////////
Should your function RETURN something? Function just doing "x+y" without returning anything is like you buying a car without a cash and without knowing prize.
/////// Write your code below ////////////////////////////
template <typename T>
void add(T x, T y)
{
x + y;
}
/////// End writing your coding //////////////////////////
(sic)
This is the most pointless function I've seen; no offence. The compound expression performed returns a result but you don't do anything with that value. Eraggo is right - you should return the result of the expression.
What would that error be, exactly? I personally would re-write you function to this:
1 2 3 4 5
template <typename T>
T add(T x, T y)
{
return( x + y );
}
Be careful when using this function as it takes any type, including structures. I you find yourself using this function on a structure, be sure the overload the addition operator within the structure.
no, this is not a homework, it's a laptest for c++ and i download it and i tried to slove, solved 6 of 10, and all the questions in this post i can't solve them. so i just need a help to understand how to solve, that's it.
all the code must be correct except the lines within "Write your code below" and "End writing your coding", so we just have to modfiy between them to fix the problems.
it's working fine for a while and then stop working. can you give more hints?
for third:
this line
1 2
: Person(name),id(id)
{};
is to access the name of the class person. the problem is becuase of private, and also i can't change it to protected, even i change it will print error output.
Problem is with your print() for 3rd program.
You have:
1 2 3 4
void print() const {
cout << name << endl;
cout << "ID = " << id << endl;
}
This gives an error because name is private in the Person class. However, there is a print() in the Person class which WILL print the name. Call the base class print() in the above code instead on line 2.
Note: You have the same issue in the 4th program. Solve it similarly
In your 2nd program you may want to allocate an array of strings in the constructor.
@Framework. Sorry if that seemed like too much info. for him. I didn't actually write any code for him.
I wonder if he'll get it with the hints I gave?
EDIT: I didn't see your most recent post (before mine) when I posted. Sorry. I may not have posted if I'd seen it.