Order of constructors

Can someone explain why is the base class constructor is called first ? I tried a few things but didn’t figure it out My code: #include <iostream> class Base1 { public: Base1() { std::cout << "Base1 constructor" << std::endl; } }; class Base2 { public: Base2() { std::cout << "Base2 constructor" << std::endl; } };… Read More Order of constructors

Addresses of array in default constructor is same but on printing comes out to be different

#include<iostream> #include<string.h> using namespace std; class Student{ int age; public: char *name; Student(int age,char *name){ this->age = age; this->name = new char[strlen(name) + 1]; strcpy(this->name,name); } void display(){ cout<<this->age<<" "<<this->name<<endl; } }; int main(){ char name[] = "abcd"; Student s1(20,name); Student s2(s1); s2.name[0] = ‘e’; //s2.name = "abdd"; cout<<&name<<endl; cout<<&s1.name<<endl; cout<<&s2.name<<endl; s1.display(); s2.display(); } OUTPUT… Read More Addresses of array in default constructor is same but on printing comes out to be different

Difference between the two style of calling a constructor function?

Hi fellow developers, I was asked to implement the following let num = new Mathematics(5).add(5).sub(8).add(8).getVal(); console.log(num) // 10 Below is my implementation: function Mathematics(val) { this.value = val; this.add = (val)=>{ this.value += val; } ; this.sub = (val)=>{ this.value -= val; } this.getVal = ()=>{ return this.value; } } Now when I run this… Read More Difference between the two style of calling a constructor function?

Using a constructor to set a number back to 1 when it goes over a certain number or below 0

so I am writing a program where when a variable called Floor goes over the topFloor it resets back to 1 or does the same when it goes below 0. I am pretty new to java and just started learning constructors so some help would be appreciated. Here is my code for the constructor and… Read More Using a constructor to set a number back to 1 when it goes over a certain number or below 0

I am expecting error that no default constructor as it is deprecated but i am not getting any error?could anyone tell?

As per cppreference doc: The generation of the implicitly-defined copy constructor is deprecated if T has a user-defined destructor or user-defined copy assignment operator. But i am not getting any error like default constructor not present.Please correct me if i am wrong. #include <iostream> class Demo { int val = 100; public: ~Demo() { std::cout… Read More I am expecting error that no default constructor as it is deprecated but i am not getting any error?could anyone tell?

When are defaults supplied in C# constructors?

I am looking at this in C#: namespace R3.Geometry { public class Tile { public Tile() { Isometry = new Isometry(); EdgeIncidences = new List<Tile>(); VertexIndicences = new List<Tile>(); } public Tile( Polygon boundary, Polygon drawn, Geometry geometry ) : this() { Boundary = boundary; Drawn = drawn; Geometry = geometry; VertexCircle = boundary.CircumCircle; }… Read More When are defaults supplied in C# constructors?

C++: no instance of constructor matches the argument list

I want to return a Piece from function. Piece is an abstract class parent for all of my pieces. Piece* Board::getPieceBySign(char sign, Player& _player1, Player& _player2, Player& _emptyPlayer, int row, int col) const { switch (sign) { case Piece::B_KING: // Example: King(Player& p, const int row, const int col, Board& b); return new King(_player1, row,… Read More C++: no instance of constructor matches the argument list

How many times is a constructor called when it's overloaded?

BankAccount.java public class BankAccount { private double checkingBalance; private double savingBalance; private static int numberOfAccounts; public BankAccount() { this(0, 0); numberOfAccounts++; } public BankAccount(double checkingInitial, double savingInitial) { this.checkingBalance = checkingInitial; this.savingBalance = savingInitial; numberOfAccounts++; } public static int getNumberOfAccounts() { return numberOfAccounts; } Test.java public class Test { public static void main(String[] args) {… Read More How many times is a constructor called when it's overloaded?