OOP basics

Jan 27, 2013 at 4:09pm
Hello all!!!!

I have a task:

The below program creates an object called “printer” in a class called “PrintClass” and then uses the object to print text.

Your task is to write the class “PrintClass” and the required method. (You don't need to copy the below code in the text box).

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
#include <iostream>

#include <string>

using namespace std;

 

// your code here

 

int main()

{

  char charstring[50];

  cout << "Input a character string for printing:";

  cin.get(charstring, 50);

  PrintClass printer;

  printer.Print(charstring);

}






So , I try to write a class and enter there before main program, but something is always wrong!


1
2
3
4
5
6
7
8
9
10
11
class PrintClass  {
public:	
	string printer;
	char charstring[50];
	void Print();
	
};
void PrintClass::Print()
{
	cout << charstring ;
}





What is wrong?
Last edited on Jan 27, 2013 at 4:10pm
Jan 27, 2013 at 4:13pm
Even though the two variables are both named charstring they both refer to different memory locations. Try replacing your print function with something like:
1
2
3
4
5
6
7
8
9
class PrintClass {
	void print(char* charstring);
};



void PrintClass::print(char* charstring) {
	cout<<charstring;
}
Jan 27, 2013 at 4:41pm
Thank you!!!!
Topic archived. No new replies allowed.