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.
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:
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];
This question already has answers here:
Defining static members in C++
(6 answers)
Closed 7 years ago.
I need a class with static std::vector<int> simples (first N simple numbers). I created it in static method __init__, which is called before any instance of MyClass is created:
class MyClass
{
public:
MyClass()
{
/* I need to use MyClass::simples here */
printf("%d\n", (int)MyClass::simples.size());
/* But I get the error here :( */
}
static void __init__(int N)
{
simples.push_back(2);
/* ...
here I compute first N simple numbers and fill
MyClass::simples with them
*/
}
private:
static std::vector<int> simples;
};
int main()
{
MyClass::__init__(1000);
MyClass var;
return 0;
}
But when I tried to use this vector in construction, I get undefined reference to 'MyClass::simples' error. How to solve my problem?
When defining a static member in C++, you need to write it two times: first in class definition as you did:
static std::vector<int> simples;
And then outside (preferably in an external .cpp file):
std::vector<int> MyClass::simples;
If you know about C language, this can help you: static members in C++ are comparable from global variables in C: defined as a prototype in .h file included whenever you need it, and value initialized in one .c/.cpp file.
You have to define the static data member outside the class
std::vector<int> MyClass::simples;
Within the class definition it is only declared.
This question already has answers here:
How to initialize private static members in C++?
(18 answers)
Closed 8 years ago.
I have a class like this:
class example {
public:
static void setStaticVar() { example::var = 1; };
private:
static int var;
};
But it gives me linker errors and I have no idea why.
I want to store some data in the variable that is the same for every instance. That's why I want to use a static variable instead of an instance variable (with an instance variable I would store the same data in every single instance of the class which is a waste of memory).
How do I do this?
In the source file
int example::var = 0;
You need to initialize the variable once. In one .cpp, outside of any functions, you have to initialize the variable:
int example::var = 0;
You must initialize it out of the class definition.
Try this.
class example { ... };
// initialize it to avoid linker errors
int example::var = 1;
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.