0

Alright everyone I am pretty new to programming in C++. I have being doing it for college for about 2 months now. I send my professor a copy of one of my programs to help me understand the different parts of it. He told me the program I have submitted would require a constructor. With that said my knowledge on constructors is they must have the same name as the class in the program. So could anyone explain to me what my constructor is in my program. Thank you

    #include <iostream>  
    #include <string>    

    using namespace std;  
    class Vertebrae {     //here i am declaring my class as Vertebrae
    private:          //here i am setting a private access modifier
      string brain;     //creating the string brain as a private attribute
      string backbone;   //creating the string backbone as a private attribute
      string skeleton;  //creating the string skeleton as a private attribute
    
    public:     //setting my public access modifier
    
    
    void setBrain(string a) {
        brain = a;
    }
    string getBrain() {
        return brain;
    }
    void setBackbone(string b) {  
        backbone = b;
    }
    string getBackbone(){
        return backbone;
    }
    void setSkeleton(string c) { 
        skeleton = c;
    }
    string getSkeleton() {
        return skeleton;
    
    }
  };

  int main(){
  Vertebrae A;   
  Vertebrae B;  
  Vertebrae C;  
  A.setBrain("Brains");  
  B.setBackbone("Spines"); 
  C.setSkeleton("Boney Skeletons");  
  cout << "Vertebrates have " << A.getBrain();  //outputting the first definition of a vertebrae
  cout << ", ";  //outputting a comma
  cout << B.getBackbone(); //outputting the second definition of a vertebrae
  cout << ", and "; //outputting another comma and the word and
  cout  << C.getSkeleton();  //outputting the third definition of a vertebrae
  return 0;
 }
  • 2
    Like the professor said, you don't have one (well you do but the compiler gives it to you since you didn't give it one). Sounds like you should read up the class/constructor chapter of a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Feb 03 '21 at 16:34
  • Well that's the thing, he told me the program did have one I just don't know what it would be. @NathanOliver – Randall Galloway Feb 03 '21 at 16:36
  • 2
    The generated default constructor for `Vertebrae` will initialise `brain`, `backbone` and `skeleton` to empty strings. The question you need to answer is - is this a vaild state for the `Vertebrae` class to be in and what does an "empty" `Vertebrae` mean ? – Richard Critten Feb 03 '21 at 16:37
  • 1
    @RandallGalloway Some constructors and operator functions are generated by the compiler automatically, if you don't specify these explicitely. – πάντα ῥεῖ Feb 03 '21 at 16:37
  • Note also that not all constructors are the same. You need to use the [member initializer list](https://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-lists) for it to be efficient. Otherwise you'll just be reassigning strings inside the body of the constructor which is no different than calling setters. – rustyx Feb 03 '21 at 17:00

2 Answers2

0

Constructor is a special method of a class which gets called when you create an object. Unless your constructor is called the object creation will be incomplete.

Class will have a default constructor if you have not created one.

Pramod
  • 1
  • 2
0

A constructor is a member function of a class that initializes objects of a class. In C++, Constructor is automatically called when an object(instance of a class) create. It is a special member function of the class. How constructors are different from a normal member function?

A constructor is different from normal functions in the following ways:

  • Constructor has the same name as the class itself
  • Constructors don’t have a return type
  • A constructor is automatically called when an object is created.
  • If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).

Furthermore, in the definition of a constructor of a class, the member initializer list specifies the initializers for direct and virtual bases and non-static data members. (Not to be confused with std::initializer_list.)

A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign an initial value to an object at the time of its creation.

There is also the copy constructor, but I'm sure that with a good C++ book as @NathanOliver said, you will be able to go deeper on this and other things too. There is a special type of Constructors that takes an object as an argument and is used to copy values of data members of one object into another object.

#include <iostream>
#include <string>

class Vertebrae {               // here i am declaring my class as Vertebrae
private:                        // here i am setting a private access modifier
    std::string brain;         // creating the string brain as a private attribute
    std::string backbone;      // creating the string backbone as a private attribute
    std::string skeleton;      // creating the string skeleton as a private attribute
    
public:     //setting my public access modifier
    
    void setBrain(std::string a) {
        brain = a;
    }
    
    std::string getBrain() {
        return brain;
    }
    
    void setBackbone(std::string b) {
        backbone = b;
    }
    
    std::string getBackbone(){
        return backbone;
    }
    
    void setSkeleton(std::string c) {
        skeleton = c;
    }
    
    std::string getSkeleton() {
        return skeleton;
    }
    
    // Default Constructor Example
    Vertebrae() {
        // Object initialization and other stuff eventually
    }
    
    // Parametrized Constructor Example
    Vertebrae(std::string brain, std::string backbone, std::string skeleton) {
        // Or use setMethod(...)
        this->brain = brain;
        this->backbone = backbone;
        this->skeleton = skeleton;
    }
    
    /*
    // Constructor with Member Initializer List
    Vertebrae(std::string brain, std::string backbone, std::string skeleton) : brain(brain), backbone(backbone), skeleton(skeleton) {
            // Other stuff eventually only
    }
     
     */
    
};

int main(){
    Vertebrae A;
    Vertebrae B;
    Vertebrae C;
    A.setBrain("Brains");
    B.setBackbone("Spines");
    C.setSkeleton("Boney Skeletons");
    
    std::cout << "Vertebrates have " << A.getBrain();  //outputting the first definition of a vertebrae
    std::cout << ", ";  //outputting a comma
    std::cout << B.getBackbone(); //outputting the second definition of a vertebrae
    std::cout << ", and "; //outputting another comma and the word and
    std::cout  << C.getSkeleton();  //outputting the third definition of a vertebrae
    std::cout << "\n";
    return 0;
}

Just some tips:

That's all. I hope that this was helpful. Cheers, Denny

D. Caruso
  • 163
  • 1
  • 3
  • 11