This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does `class HelloWorld : public Gtk::Window` mean?
I'm new to C++, but looked at a few tutorials and read partially Accelerated C++ ... but have seen something like
class expr_t : public ast_container {
public:
virtual double eval() const = 0;
...
What does the 1st line mean? Its declaring a class named expr_t? Then whats ast_container part?
Then on line 3: virtual double eval() const = 0 what does it mean?
Seems like its declaring a virtual function called eval that returns a double, but then whats the const = 0 part?
It means that the parent class/super class/(whatever one calls it) of the expr_t class is the ast_container class. At first glance, it roughly means that everything ast_container can do, expr_t can do it as well, and something more too.
Related
This question already has answers here:
C++ class declaration after using it
(3 answers)
What are forward declarations in C++?
(8 answers)
How to create two classes in C++ which use each other as data?
(2 answers)
recursive definition in CPP [duplicate]
(5 answers)
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 7 months ago.
First of all, I think this problem has already been discussed and is pretty common but id doesn't seem to find an answer appropriate to my problem.
My problem is related to circular dependencies, which by itself can be solved by using forward declaration, however, in my case, I need to know more
then the class name in the forward declaration (like attributes and functions).
In my code both classes, Time and Distance/Speed need to know each other and each other attributes. I avoided the problem by declaring an "Interface" of Time which contains the attribute that is needed for Distance/Speed, but I'm wondering if there is a more elegant solution to this problem that wouldn't involve creating an "Interface".
class ITime
{
public:
ITime(float s) :time_s(s) {};
float time_s;
};
class Speed
{
public:
Speed(float ms = 0) :speed_ms(ms) {}
float speed_ms;
};
class Distance
{
public:
Distance(float m = 0) :distance_m(m) {}
float distance_m;
Speed operator/ (const ITime& t) const
{
return Speed(distance_m / t.time_s);
};
};
class Time :public ITime
{
public:
Time(float s):ITime(s) {}
Distance operator *(const Speed& speed) const
{
return Distance(time_s * speed.speed_ms);
}
};
This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 8 months ago.
I wonder if I can directly convert the public class to a struct. Is it permissible?
class car
{
public:
int vno;
float count;
char dname[15],x,l[50];
void input()
Is it okay just to convert to struct directly like this?
struct car
{
int vno;
float count;
char dname[15],x,l[50];
void input()
will it be run with no error? Or there is another way...
A class has private fields by default, with no keywords such as "private" and "public". If you add these attributes, you can manipulate fields' visibilities.
A struct, on the other hand, has public fields by default and we do not have to add keywords of the type public; but we can also have private data in the structure after the private keyword.
This question already has answers here:
What is this weird colon-member (" : ") syntax in the constructor?
(14 answers)
Closed 7 years ago.
I have a couple of syntax questions that I'm sure most people could answer but this is all foreign to me and I worked through a class tutorial but it left a few questions unanswered.
This code is the declaration line of a class function and inside the parenthesis are the values to be passed. correct?
void HwCounter_IVBNHSX_IMC::SetRegisterLocations(int bus, int ha, int chan, int counterNumber)
{
_ha = ha;
_chan = chan;
_counterNumber = counterNumber;
_bus = bus;
}
In this example what does the additional semicolon at the end enable? Where would I be looking to see what the counterNumbers are associated with?
HwCounter_IVBNHSX_IMC::HwCounter_IVBNHSX_IMC(int hwType, const char* pName) : HwCounterBase(pName)
{
_counterNumber = 0;
_currentConfig = 0;
_hwType = hwType;
}
I'm unable to post the entire source code sorry and I know that makes it more difficult but any help would be appreciated.
This code is the declaration line of a class function and inside the
parenthesis are the values to be passed. correct?
Yes, it is, being understood that the function has to be first declared inside the class.
In this example what does the additional semicolon at the end enable?
HwCounter_IVBNHSX_IMC::HwCounter_IVBNHSX_IMC(..) is a constructor for the class HwCounter_IVBNHSX_IMC.
The : is followed by a list of mem-initializer, a special form of initialization of data members and of the base class if necessary. For example HwCounterBase(pName) means that the data member (or the base class) HwCounterBase is intialized by calling its constructor with the value pName.
This:
void HwCounter_IVBNHSX_IMC::SetRegisterLocations(int bus, int ha, int chan, int counterNumber)
{
...
}
is the definition of a function. (The declaration is something else, and to learn the distinction you should start with a simpler example.) Its name is SetRegisterLocations, it is a member of the class HwCounter_IVBNHSX_IMC, it takes four arguments (all int), and it returns nothing (void).
This:
HwCounter_IVBNHSX_IMC::HwCounter_IVBNHSX_IMC(int hwType, const char* pName)
{
...
}
is similar, but it is a constructor. The name of the function is the same as the name of the function, and it has no return type (not even void).
This:
HwCounter_IVBNHSX_IMC::HwCounter_IVBNHSX_IMC(int hwType, const char* pName) : HwCounterBase(pName)
{
...
}
is the same, but it has an initializer list (consisting of only one initializer) which sets the value (or calls the constructor) of a member variable (HwCounterBase).
Where would I be looking to see what the counterNumbers are associated
with?
The rest of the code.
This question already has answers here:
Error logging in c++
(3 answers)
Closed 7 years ago.
I have a Logger class in my C++ application. This class has public methods like writeDebug(std::string & str) , can write to a debug output file and it works for me very good. The object is being created on application start by the first main class and there it is stored as a Logger * myLogger; object.
Since I have other classes as well, I would like to provide this logging functionality to those classes as well.
Currently I oberhead the myLogger pointer to toher classes in their contructor, and all the other classes store this very same Logger pointer when being created --> all other classes has this Logger * myLogger pointer stored:
Other_XY_Class::Other_XY_Class(Logger * logger, ...)
: localLoggerPtr{logger} { //.. }
==> localLoggerPtr (in class Other_XY_Class) == myLogger (in main application class)
I can imagine that there is a more convenient / elegant way to handle that ?
Since you only have one instance, why not use a Singleton for that?
Something along this lines:
class foo {
private:
void bar();
static foo Instance;
public:
static void Bar() { Instance.bar(); }
}
This question already has answers here:
Unresolved external symbol on static class members
(6 answers)
Closed 7 years ago.
I'm currently designing some code on QT that is built across multiple source. I want to create an array in one source and be able to access it in another source.
Currently in my Header I have class
Array_Class : public QString
{
public:
static QString Data_Array [2];
};
I don't think I need a constructor as I'm going to "populate" the array before I read it.
currently in my source.cpp I have
Array_Class::Data_Array[0]= "foo";
Array_Class::Data_Array[1]= "bar";
however this gives me the error message undefined reference to "Array_Class::Data_Array". What am I missing? thanks
So far, you have only declared your array:
Array_Class : public QString
{
public:
static QString Data_Array [2]; // -> only a declaration!
};
In order to use it, you must now define it. To do this, you need to place somewhere in your .cpp:
QString Array_Class::Data_Array [2];