FAQ: Classes and Objects - Review

This community-built FAQ covers the “Review” exercise from the lesson “Classes and Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, I was just wandering if there is a way to delete a particular element in a vector unlike the pop_back() method which only deletes the last element in a vector?
A reply would be appreciated.

1 Like

Hello there, is it a good idea to use a switch statement in a while or a for loop considering that there will be break statements in the switch cases ?

Hello you all! I need some help here. Why my code doesn’t work?
Everything was fine until I added constructor, destructor and moved declarations and definitions to separate files.
Thanks!

You probably need to do in a function by copying the vector to another vector except the element(s) of which you define their indexes to be ignored. Then, at then end, you have your new vector without those element(s).

1 Like

beside those problematic set_xxx() methods, I think there were some weird chars somewhere as spaces that were making the issue. I cleaned up your code and it’s working. Have a look and compare it with yours.

/////////////////////////////////////////main.cpp
#include <iostream>
#include "your_class.hpp"
// include your header file here:


int main() {

 Pet pet1("Willy", "dog", "Schnider", 5);

 std::cout << pet1.get_name() << "\n";
 std::cout << pet1.get_type() << "\n";
 std::cout << pet1.get_owner() << "\n";
 std::cout << pet1.get_age() << "\n";
}

/////////////////////////////////////////your_class.hpp
#include <iostream>
#include <string>

using namespace std;

class Pet {
  // attributes
  std::string name;
  //std::string type;
  std::string type;
  std::string owner;
  int age;

  public:
  // add constructor
    Pet(std::string new_name, std::string new_type, std::string new_owner, int new_age);
    

  // add destructor
    ~Pet();

  // methods
  /*void set_name(std::string new_name);

  void set_type(std::string new_type);

  void set_owner(std::string new_owner);

  void set_age(int new_age);*/

    std::string get_name();

    std::string get_type();

    std::string get_owner();

    int get_age();

  //void print_data();
};


/////////////////////////////////////////your_class.cpp
#include <iostream>
#include <string>
#include "your_class.hpp"

using namespace std;


Pet::Pet(string new_name, string new_type, string new_owner, int new_age){   
   name = new_name;
   type = new_type;
   owner = new_owner;
   age = new_age;
 }

 
 Pet::~Pet()
 {
   std::cout << "Object is destroyed."<<endl;
 }
/*
void Pet::set_name(std::string new_name){
    name = new_name;
  }

void Pet::set_type(std::string new_type){
    type = new_type;
  }

void Pet::set_owner(std::string new_owner){
    owner = new_owner;
  }

void Pet::set_age(int new_age){
    age = new_age;
  }
*/
string Pet::get_name(){
    return name;
  }

string Pet::get_type(){
  return type;
}

string Pet::get_owner(){
    return owner;
  }

int Pet::get_age(){
    return age;
  }

/*void Pet::print_data(){
     std::cout << name << ", ";
     //std::cout << type << ", ";
     std::cout << owner << ", ";
     std::cout << age << "\n";
  }*/

For the sake of the review, I understand the basics of classes and objects. I wanted ask is it is possible for a object instantiation(without constructor) and a object instantiation with a constructor to exist together in a functioning program if that have the same parameters and arguments? If yes, then why does my could not work? I have to comment out either the object instantiation code along with corresponding member function declaration and definition or the object instantiation with constructor declaration and definition. Thanks for your time and consideration.

Here is my code:

Blockquote // int main.cpp

#include
// include your header file here:
#include “your_class.hpp”

// int main.cpp
int main() {

//object instantion without constructor
/*Student devin_rainey;
devin_rainey.add_student_info(“Devin Rainey”, “Male”, 27, 3.5);
std::cout << "Full name: " << devin_rainey.get_student_name() << “\n”;
std::cout << "Sex: " << devin_rainey.get_student_sex() << “\n”;
std::cout << "Age: " << devin_rainey.get_student_age() << “\n”;
std::cout << "GPA: " << devin_rainey.get_student_gpa() << “\n”; */

//object instantion with constructor
Student devan(“Devan”, “Female”, 25, 4.25);
std::cout << "Full name: " << devan.get_student_name() << “\n”;
std::cout << "Sex: " << devan.get_student_sex() << “\n”;
std::cout << "Age: " << devan.get_student_age() << “\n”;
std::cout << "GPA: " << devan.get_student_gpa() << “\n\n”;

}

Blockquote //your_class.hpp

#include

class Student {
//attributes
std::string name;
std::string sex;
int age;
double gpa;

public:
//methods aka member functions

//Construtor
Student(std::string new_name, std::string new_sex, int new_age, double new_gpa);
//Destructor
~Student();

//void add_student_info(std::string new_name, std::string new_sex, int new_age, double new_gpa);

std::string get_student_name();

std::string get_student_sex();

int get_student_age();

double get_student_gpa();

std::string get_military();

};

Blockquote //your_class.cpp

#include “your_class.hpp”

//Constructor definition
Student::Student(std::string new_name, std::string new_sex, int new_age, double new_gpa)

: name(new_name), sex(new_sex), age(new_age), gpa(new_gpa) {}

//Destructor definition
Student::~Student () {

std::cout << “Program Ended \n”;
}

/*void Student::add_student_info(std::string new_name, std::string new_sex, int new_age, double new_gpa) {

name = new_name;
sex = new_sex;
age = new_age;
gpa = new_gpa;

} */

std::string Student::get_student_name() {

return name;
}

std::string Student::get_student_sex() {

return sex;
}

int Student::get_student_age() {

return age;

}

double Student::get_student_gpa() {

return gpa;

}

Fun project! I love making data types, so I did this one for sure!

Here’s my code, if you can’t tell, I made a Book Data Type:

// main.cpp
#include <iostream>

// include your header file here:
#include "Book.hpp"

int main() {

  // Make a few book objects:
  Book holes("Holes", "Louis Sachar", "Life was not easy. Elya worked hard, but bad luck seemed to follow him everywhere. He always seemed to be in the wrong place at the wrong time.");

  Book fahrenheit_451("Fahrenheit 451", "Ray Bradbury", "‘Why is it,’ he said, one time, at the subway entrance, ‘I feel I’ve known you so many years?’ ‘Because I like you,’ she said, ‘and I don’t want anything from you.’");

  // ... Make more books but I'm Lazy

  Book library[2] = {holes, fahrenheit_451};

  for (Book b : library) {

    b.print_contents();

  }

  return 0;
  
}
/// Book.hpp
#include <string>

// Custom book data type
class Book {

  public:
    Book(std::string title, std::string author, std::string contents);
    std::string get_author();
    std::string get_title();
    void print_contents();
    
  
  private:
    std::string title;
    std::string author;
    // Yeah yeah, I know it won't fit a whole book
    std::string contents;

};
// Book.cpp
#include <string>
#include <iostream>

#include "Book.hpp"

// Constructor
Book::Book(std::string title, std::string author, std::string contents) {

  this->title = title;
  this->author = author;
  this->contents = contents;

}

std::string Book::get_author() {

  return author;

}

std::string Book::get_title() {

  return title;

}

void Book::print_contents() {

  std::cout << contents << std::endl;

}

Have fun, and happy coding!

No, from what I think you’re trying to say, you cannot. I believe you are trying to create an object with no constructor (eg. Classname name;) which will give a compiler error. C++ will not let you create an object with no Constructor when the class has a Constructor present.

just thought id post the data type i made, my main goal is to make my own game. thoughts on this little expo after the lesson? lmk what you think!

this i what the output looks like when executing:

image

If you provide default inputs in the class creation, it should work. Example here (you can run it!):

#include <iostream> #include <string> class Student { //attributes std::string name; std::string sex; int age; double gpa; public: //methods aka member functions //Construtor Student(std::string new_name = "none", std::string new_sex = "none", int new_age = 0, double new_gpa = 0); //Destructor ~Student(); void add_student_info(std::string new_name, std::string new_sex, int new_age, double new_gpa); std::string get_student_name(); std::string get_student_sex(); int get_student_age(); double get_student_gpa(); std::string get_military(); }; // int main.cpp int main() { //object instantion without constructor Student devin_rainey; devin_rainey.add_student_info("Devin Rainey", "Male", 27, 3.5); std::cout << "Full name: " << devin_rainey.get_student_name() << "\n"; std::cout << "Sex: " << devin_rainey.get_student_sex() << "\n"; std::cout << "Age: " << devin_rainey.get_student_age() << "\n"; std::cout << "GPA: " << devin_rainey.get_student_gpa() << "\n"; //object instantion with constructor Student devan("Devan", "Female", 25, 4.25); std::cout << "Full name: " << devan.get_student_name() << "\n"; std::cout << "Sex: " << devan.get_student_sex() << "\n"; std::cout << "Age: " << devan.get_student_age() << "\n"; std::cout << "GPA: " << devan.get_student_gpa() << "\n\n"; Student Terrence; std::cout << "Full name: " << Terrence.get_student_name() << "\n"; std::cout << "Sex: " << Terrence.get_student_sex() << "\n"; std::cout << "Age: " << Terrence.get_student_age() << "\n"; std::cout << "GPA: " << Terrence.get_student_gpa() << "\n\n"; } //Constructor definition Student::Student(std::string new_name, std::string new_sex, int new_age, double new_gpa) : name(new_name), sex(new_sex), age(new_age), gpa(new_gpa) {} //Destructor definition Student::~Student () { std::cout << "Program Ended \n"; } void Student::add_student_info(std::string new_name, std::string new_sex, int new_age, double new_gpa) { name = new_name; sex = new_sex; age = new_age; gpa = new_gpa; } std::string Student::get_student_name() { return name; } std::string Student::get_student_sex() { return sex; } int Student::get_student_age() { return age; } double Student::get_student_gpa() { return gpa; }

Do not provide defaults in the definition if you provide them in the declaration! I don’t think providing them in the definition works at all, but I haven’t tested it.

Also, lots of your quotation marks don’t get properly recognized for some reason and I had to fix them.

It’s a fine idea, but as a note, if you were to do it that way in a game, it would clear the player’s data entirely from whatever you’re running the game on whenever you close it.