C++ vs Java (Calling super constructor)

Hello,

I'm having trouble trying to call the super constructor in C++ from a derived class. There are two different cases:

1. As you may know, the way to do this in Java is by using super keyword in the constructor of the child class, I guess I'm making a mistake because even though my C++ code does print the Hello World statement, it prints it twice.

Here are the two codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Java
class A{
	public A(){
		System.out.println("Hello world!");
	}
}

class B extends A {
	
	public B(){
		super();
	}
}

class Main{
	public static void main(String args[]){
		new B();	
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//C++
#include <iostream>

using namespace std;

struct Super{
	Super(){
		cout << "Hello world" <<endl;
		}
};

struct Sub : public Super{
	Sub(){
		Super();
	}
};

int main(){
	Sub sub;
}


2. The second case is about values passed as parameters. Again, it works in Java but not in C++. I'm obviously doing something wrong in C++ in both situations, can anybody help me out? thanks a lot.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Java

class A{
	int x;
	public A(int x){
		this.x = x;
		System.out.println("Value: " + 5);
	}
}

class B extends A {
	
	public B(){
		super(5);
	}
}

class Main{
	public static void main(String args[]){
		new B();	
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//C++
#include <iostream>

using namespace std;

struct Super{
	int x;
	Super(int x){
		this-> x = x;
		cout << "Value: " + x <<endl;
		}
};

struct Sub : public Super{
	Sub(){
		Super(5);
	}
};

int main(){
	Sub sub;
}
First one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

struct Super{
    Super(){
        cout << "Hello world\n";
        }
};

struct Sub : public Super{
};

int main(){
    Sub sub;
}


(you could of course write an empty constructor for Sub, somthing like Sub() {} or even Sub() : Super() {}, but that's exactly what the compiler does in this case, so there's no reason to)

Second:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

struct Super{
    int x;
    Super(int x) : x(x) {
        cout << "Value: " <<  x << '\n';
    }
};

struct Sub : public Super{
    Sub() : Super(5) {
    }
};

int main(){
    Sub sub;
}


Remember, in C++ a class is not limited to having just one parent, so there is no special word for *the* parent. Just use the name of the particular base class you want to initialize in the constructor.
Last edited on
1
2
3
4
5
struct Sub : public Super{
	Sub(){
		Super();
	}
};


This constructs a temporary Super object in the Sub constructor which is immediately destroyed.


1
2
3
4
struct Sub : public Super{
	Sub() : Super() {
	}
};


is the way to do it explicitly.


1
2
3
4
struct Sub : public Super{
	Sub(){
	}
};


is equivalent.


In the second case you're running afoul of pointer arithmetic. "Value: " is a pointer. Adding a number to a pointer yields another pointer.

1
2
3
4
5
6
7
8
9
	Super(int x) : x(x) {
		cout << "Value: " << x <<endl;
	}

                ...

	Sub() : Super(5) {
	}
 





In case 1 you should change lines 13 to 15 as:

1
2
3
   Sub() : Super()
   {
   }



and in case 2 change lines 15 to 17 as:

1
2
3
   Sub() : Super(5) 
   {
   }


or to improve on it and make it more usefule you could do:

1
2
3
   Sub(int x) : Super(x)            //now the hardcoded 5 is gone
   {
   }
Hello SIK... well i have no more knowledge about this topic but i think you are saying right... i am totally agree with you....,

Hey guys thank you very much. Very useful contribution!!! that's what I needed.
Topic archived. No new replies allowed.