Reputation:
I know java and i learning now c++.I learn it more easier than other language it has many same things.My question is in class in a book there is a full constructor but i didn't face the default constructor anywhere.Has c++ default constructor,if yes should i write it?Additional to,i want to test and something,on this class there is in public:
and down of it there are variables and after some time there is private:
and down of it there are some variables too.Is like java public and privates variables? but instead of write private int numbers;
on c++ we write
private:
int numbers;
int coffee;
Am i right?
Upvotes: 0
Views: 2823
Reputation: 20396
The fact that your teacher was marking points off for not including a Default Constructor is... interesting, to say the least.
As a general principle, both in Java and C++, a Constructor is responsible for initializing the object to a fully formed state. The reason to have a Default Constructor is to enable a fully formed object to be constructed without any explicit input. But that can get weird:
//Java
public class Student {
public String name;
public int age;
public Student() {
this("", 0);
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
//C++
class Student {
public: //Everything after this point has 'public' access modifier; is like putting
//'public' in front of each variable and method
std::string name;
int age;
Student() : Student("", 0) {}
Student(std::string name, int age) : name(std::move(name)), age(age) {}
};
In this example, Student
has a Constructor which initializes the object with provided values, and a Default Constructor which initializes the object to have an empty name and an age of 0.
But think about that: does that make sense? Does it make sense for a valid Student
object to have no name, or for it to be possible to construct a Student
object if you don't know their name? Forgetting the functional requirements for a moment (i.e., arrays can be difficult to construct if the object doesn't have a Default Constructor), it can be logically inconsistent to have an object that can be Constructed without input like that. Depending on your use-case, it might make more sense to not have a Default Constructor.
So determining whether to include a Default-Constructor in your code is a matter of design principles, and has nothing to do with whether you're using Java or C++, or most programming languages for that matter.
Regarding your other concerns:
public:
, protected:
, and private:
have different rules than in Java (public
and private
are mostly the same, protected
is the odd-one out, and Java's default
doesn't exist in C++, but can be emulated through the use of the friend
keyword), but their behavior is easy to identify:
class example {
//private:
//'class' declarations have everything 'private' by default, 'struct' makes things 'public' by default
int val; //is private
std::string type; //is private
public: //Will be public until another access modifier is used
example() {} //is public
example(int val, std::string type) : val(val), type(std::move(type)) {} //is public
void do_something() { //is public
private_function();
}
private: //Will be private until another access modifier is used
void private_function() { //is private
val++;
}
};
In Java, you'd write the same code like this:
public class example {
private int val;
private String type;
public example() {}
public example(int val, String type) {
this.val = val;
this.type = type;
}
public void do_something() {
private_function();
}
private void private_function() {
val++;
}
}
Upvotes: 0
Reputation: 6131
It's hard to tell exactly what you are asking. But: 1) A default constructor is generated for you if you don't create any constructors yourself. It's public by default. 2) The generated default constructor will default construct the base classes and members of the class in declaration order (virtual bases (if any), depth first, right to left in order of declaration, then normal bases in left to right order of declaration, then member variables in order of declaration. If any of these cannot be default constructed, then the default constructor cannot be generated for you. 3) If there are const members or reference members, or members that do not have default constructors, or your class has bases that do not have default constructors, the default constructor cannot be generated since those members must be initialized with one or more values. 4) If you define a constructor, and you also want the compiler to generate a default constructor for you, and a default constructor is valid in your code, do this:
class MyClass
{
public:
MyClass() = default; // explicitly tell compiler to generate a default
MyClass(int x) { ...} // normally would suppress gen of default ctor
};
If I understand your question about access specifiers, they are like labels, and everything that follows them has that access specification until you write another one that changes it. In a class, by default access is private. In a struct, access is public.
Hope this helps.
Upvotes: 1