Please can anyone tell me in few words What is wrong with the following class definition? whait's missing, etc.
Class Numeric
{
private:
long x;
public
...
void set (long n) const { x = n; }
long get () { return x; } const
} Please can anyone tell me in few words What is wrong with the following class definition? whait's missing, etc.
Class Numeric
{
private:
long x;
public
...
void set (long n) const { x = n; }
long get () { return x; } const
} A compact checklist and clarifications that build on , and 's observations, plus answers to the extra exercises.
Syntax checklist: C++ is case‑sensitive (use class, not Class); access specifiers require a colon (public:); a class definition must end with a semicolon; put const after the parameter list when declaring a const member function. Setters should not be declared const unless there is a specific reason.
Const correctness (semantics): a member function marked const promises not to modify observable object state. To allow a const method to change a particular implementation detail (e.g., a cache or debug counter), mark that data member mutable. This is intended for non‑semantic state only; overuse breaks the meaning of const.
class Num {
mutable long cache; // for caching only
public:
void record(long v) const { cache = v; } // allowed because cache is mutable
long read() const { return cache; }
}; static so it can be referred to without an instance. The static member still needs one out‑of‑class definition in exactly one translation unit.class X {
public:
static int common;
};
int X::common = 0; // define in a .cpp file
// then functions can use X::common directly bool func1(double, double&, double&); use res = func1(x, y, z); (references bind to variables). For bool func2(double, double*, double*); use res = func2(x, &y, &z); (pass addresses).std::string strReverse(const std::string& s) {
return std::string(s.rbegin(), s.rend());
} If the assignment forces a reference return, either return a reference to caller‑provided storage or to a function‑static string (the latter is not thread‑safe and generally discouraged).
These points resolve the syntax errors flagged earlier and explain the underlying reasons (const semantics, static members, lifetime rules).
Jump to Post— venomxxl 24Public 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?
Jump to Post— venomxxl 24I'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 …
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?
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.
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.
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.
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 ?
and you have forgot the ':' after the 'public' :)
in your first post, you need to end the class with a ; after the closing brace
class name {
...
}; you would access it by creating an instance of the class in a declaration,
class clName{
...
public:
char* publicMember;
}; 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...
clName objectName; access members of the object's class using a period after the object name.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.