some qustion in classes

Pages: 12
hello, i have some programes and i want to correct them to work fine

i can only modfiy the code within the the line // Write your code below

first programe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/********************************************************
 +---------------------------+
 | Question  2 ( 2.0 marks ) |
 +---------------------------+
Write a template version of function "add" to make the program  
below produce the following output:
5 + 3 = 8
1.1 + 2.2 = 3.3
M + U = MU
Multimedia + University = MultimediaUniversity

*********************************************************/

#include <iostream>
using namespace std;

/////// Write your code below ////////////////////////////
template <typename T>
void add(T x, T y)
{
	 x + y;
	
}
/////// End writing your coding //////////////////////////

int main() {
  int a1 = 5, a2 = 3;
  double b1 = 1.2, b2 = 2.3;
  char c1 = 'M', c2 = 'U';
  string d1 = "Multimedia", d2 = "University";

  cout << a1 << " + " << a2 << " = " << add (a1, a2) << endl
       << b1 << " + " << b2 << " = " << add (b1, b2) << endl
       << c1 << " + " << c2 << " = " << add (c1, c2) << endl
       << d1 << " + " << d2 << " = " << add (d1, d2) << endl;

  return 0;
}


in this programe i have to use template to accept any datatype, i tried to fix that programe but still can't complie.



second programe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*************
 +------------------------------+
 | Question  5 ( 2.0 students ) |
 +------------------------------+
Correct the constructor to produce following output:
Students = Abu Ali Atok
Students = Baba Belal Bob

*********************************************************/
#include <iostream>
using namespace std;

const int SIZE = 3;
class Course {
    string* students;
  public:
    Course (string* students);
    ~Course ();
    void print();
};

Course::Course (string* students) {
/////// Write your code below ////////////////////////////
cout << "Students = ";
  for (int i = 0; i < SIZE; i++)
     cout << students[i] << " ";
     cout << endl;


/////// End writing your coding //////////////////////////
}

Course::~Course() {
  delete [] students;
}

void Course::print() {
  cout << "Students = ";
  for (int i = 0; i < SIZE; i++)
     cout << students[i] << " ";
  cout << endl;
}

int main() {
  string s1[] = {"Abu", "Ali", "Atok"};
  Course c1(s1);
  c1.print();
  string s2[] = {"Baba", "Belal", "Bob"};
  Course c2(s2);
  c2.print();

  return 0;
}


when i cmpile this programe it's only print the first output and then stop working.



third programe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*************************
 +---------------------------+
 | 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>
using namespace std;

class Person {
  string name;
 public:
  Person (string name)
    : name(name) {} 
  void print() const {
    cout << "Name = " << name << endl; 
  }
};
class Student : public Person {
  const int 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.


last questio:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**************
 +---------------------------+
 | 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>
using namespace 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?

thank you and waiting for you
1
2
3
4
5
6
7
8
/////// 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.

Try to return something T. :P

hello eraggo,

it's void function, the void function must not have any return value.
closed account (zb0S216C)
empror9 wrote:
1
2
3
4
5
6
7
8
/////// 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.

Wazzak
hello framework,

i tired to return in this function, but still have compile error.

closed account (zb0S216C)
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.

Wazzak

Last edited on
ok that's works!

for me i used
1
2
3
4
5
6
 template <typename T>
void add(T x, T y)
{
	return x + y;

}


so my problem was the void, it should be T. what about the other question? no idea?
closed account (zb0S216C)
---[Post revoked]---

Wazzak

Last edited on
no, i can only fix any lines whithin

1
2
3
4
5
6
/////// Write your code below ////////////////////////////
  
  //here only


/////// End writing your coding ////////////////////////// 
closed account (zb0S216C)
Homework, eh? I'll only hint you then, since telling you straight up won't be beneficial to you.

Second Program:
Something is missing. What does Course::~Course( ) tell you?

Third Program:
Your class initializer list doesn't look quite right. I don't see a member of Student named Person.

Last Program:
Hmmm... What's the name of something that ins't physical but does exist? Hmmm I think it begins with v.

Wazzak
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.

closed account (zb0S216C)
My latter point still stands. try following my hints. If you need more, ask :)

Wazzak
yes i know but all your hints about modfiyng lines that not within "Write your code below" and "End writing your coding".
closed account (zb0S216C)
They are. All the above hints I provided only modify the code within the specified area; assuming you reading the correct post.

Wazzak
ok, for Second Program:

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.

last programe:

really no idea

closed account (zb0S216C)
Second Program:
What is the opposite of delete[ ]? Having said that, a certain member of Course needs to be dynamically created.

Third Program:
Rename Student::print( ). Then, you need to call a certain method.

Last Program:
Switch private with another access-specifier.

If you truly give up, I'll give you the answers :)

Wazzak
Last edited on
closed account (D80DSL3A)
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.
Last edited on
closed account (zb0S216C)
There goes the idea of thinking. Well done, fun2code, truly.

Wazzak
Last edited on
closed account (D80DSL3A)
@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.
Last edited on
second programe:

i gave up for this problem :(

third: i solve problem by calling the Person::print in Student::print.

the last programe, do you mean i change id to be protected? i did so but only output for staff class

Pages: 12