943,839 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 788
  • C++ RSS
Feb 17th, 2009
0

What is wrong with the following class definition?

Expand Post »
Please can anyone tell me in few words What is wrong with the following class definition? whait's missing, etc.
C++ Syntax (Toggle Plain Text)
  1. Class Numeric
  2. {
  3. private:
  4. long x;
  5. public
  6. ...
  7. void set (long n) const { x = n; }
  8. long get () { return x; } const
  9. }
Last edited by Ancient Dragon; Feb 18th, 2009 at 9:00 am. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viki0077 is offline Offline
21 posts
since Feb 2009
Feb 17th, 2009
0

Re: What is wrong with the following class definition?

Public is missing a colon, and get() method should look like this:
long get () const { return x; }
Why do you need set method to be const anyway?
Reputation Points: 34
Solved Threads: 7
Junior Poster in Training
venomxxl is offline Offline
72 posts
since Jan 2009
Feb 17th, 2009
0

Re: What is wrong with the following class definition?

I'm not a C++ programmer. I'm a student and I got this question for home assignement and we just started to learn C++ and I couldn't find the answer anywhere. Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viki0077 is offline Offline
21 posts
since Feb 2009
Feb 17th, 2009
0

Re: What is wrong with the following class definition?

I would appreciate if you or anyone else would have a good will and explain me several more questions. We actually don't need to write programs just to explain some things in c++ (classes)
this question for example

6. Suppose class X includes a data elements common declared as public. How can it be
accessed by all functions in the same name space ?

and these first 2 ones ..

1. Consider the following declarations
bool func1 (double, double&, double&);
bool func2 (double, double*, double*);
double x = 9.7, y = 0.0, z = 0.0;
bool res;
Call each function with arguments x, y, and z. The return value shall be assigned to the
variable res.

2. Please write and test a function strReverse() that reverses the order of characters in a given
string. The string is passed to the function as a read-only reference. The function returns a
read-only reference to the result.


I will need to present so all help would be appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viki0077 is offline Offline
21 posts
since Feb 2009
Feb 17th, 2009
0

Re: What is wrong with the following class definition?

I'll tell you how to get the strReverse() done. Since this is a home assignment I'll only tell you how you could do it.

First of all you need to decide whether you want to do it the string or char * (C-string) way.

If I was to do this in C-string way (the way I usually do it personally) I'd first get the text length. Next I'd make a loop that puts all characters backwards to a newly allocated char *, for example:
for(int i=length;0<=i<=length;i--){ /*your code*/ }
If this works then you almost have it done actually.
Of course you could find the solution easily on the web, but this isn't the way it should be done, is it?

Hope this helps.
Last edited by venomxxl; Feb 17th, 2009 at 3:11 pm.
Reputation Points: 34
Solved Threads: 7
Junior Poster in Training
venomxxl is offline Offline
72 posts
since Jan 2009
Feb 17th, 2009
0

Re: What is wrong with the following class definition?

OK. I'll try to solve this. If anyone else can help me it would be great. But, can you explains me this under 6. 6. Suppose class X includes a data elements common declared as public. How can it be
accessed by all functions in the same name space ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viki0077 is offline Offline
21 posts
since Feb 2009
Feb 17th, 2009
0

Re: What is wrong with the following class definition?

and you have forgot the ':' after the 'public'
MHC
Reputation Points: 24
Solved Threads: 0
Newbie Poster
MHC is offline Offline
5 posts
since Feb 2009
Feb 18th, 2009
0

Re: What is wrong with the following class definition?

in your first post, you need to end the class with a ; after the closing brace
C++ Syntax (Toggle Plain Text)
  1. class name {
  2. ...
  3. };
you would access it by creating an instance of the class in a declaration,
cpp Syntax (Toggle Plain Text)
  1. class clName{
  2. ...
  3. public:
  4. char* publicMember;
  5. };
for example.

Now, you want to set the value "hello!" to the char* publicMember.
How would you do that if you had declared char* publicMember in the scope you were working in? It works the same, you just have to identify that publicMember is a "member" of a class. This is not a static variable, so first you need an object, declared as an instance of your class...
C++ Syntax (Toggle Plain Text)
  1. clName objectName;
access members of the object's class using a period after the object name.
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How do I get classes within a class???
Next Thread in C++ Forum Timeline: 2D Vector Memory Problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC