Answers for "C++ constructor"

1

c++ class member initialization

class Test  {
private:
  std::string data;
public:
  test(std::string data) : data(data) {}
  std::string getData() {
    return data;
  }
};

int main() {
  Test test("This is just some random Text");
  std::cout << test.getData() << std::endl;
  return 0;
}
Posted by: Guest on June-10-2020
3

how to initialize the object in constructor in c++

BigMommaClass {
    BigMommaClass(int, int);

private:
    ThingOne thingOne;
    ThingTwo thingTwo;
};

BigMommaClass::BigMommaClass(int numba1, int numba2): thingOne(numba1 + numba2), thingTwo(numba1, numba2) {
// Code here
}
Posted by: Guest on November-01-2020
6

cpp class constructor

class MyClass {     // The class
  public:           // Access specifier
    MyClass() {     // Constructor
      cout << "Hello World!";
    }
};
Posted by: Guest on January-05-2021
4

constructor c++ struct

struct Rectangle {
    int width;  // member variable
    int height; // member variable

    // C++ constructors
    Rectangle()
    {
        width = 1;
        height = 1;
    }

    Rectangle( int width_  )
    {
        width = width_;
        height = width_ ;
    }

    Rectangle( int width_ , int height_ )
    {
        width = width_;
        height = height_;
    }
    // ...
};
Posted by: Guest on November-10-2020
1

c++ class member initializer list

#include <iostream>
class Entity {
private : 
	std::string m_Name;
	int m_Score;
	int x, y, z;
public:
	Entity()
		:m_Name("[Unknown]"),m_Score(0),x(0),y(0),z(0)//initialize in the order of how var are declared
	{
	}
	Entity (const std::string& name) 
		:m_Name(name)
	{}
	const std::string& GetName() const { return m_Name; };
};
int main()
{
	Entity e1;
	std::cout << e1.GetName() << std::endl;
	Entity e2("Caleb");
	std::cout << e2.GetName() << std::endl;
	std::cin.get();
}
Posted by: Guest on August-09-2020
0

C++ constructor

class Book {public:    Book(const char*);    ~Book();    void display();private:    char* name;};
Posted by: Guest on June-20-2021

Browse Popular Code Answers by Language