简体   繁体   中英

Class Constructor in C++

I'm trying to write a constructor that takes optional parameters for student ID number, and first and last names of students. If they aren't provided the ID number defaults to 0 and the first and last names both default to the empty string. I am completely new to constructors so I have no clue what i'm doing but this is what I have so far...

#include <iostream>
#include <cstdlib>
#include <string>


class Student
{
public:
    Student(int idnumber, string fname, string lname);

For some reason its saying string is undefined? Also, could I use a couple if statements to have ID default to 0 and the names to the empty string if information isn't provided? Please dumb everything down for me as much as possible, I am extremely new to C++. Thanks for your time.

This is the data i'm working with... All names and scores are made up.

10601   ANDRES HYUN 88 91 94 94 89 84 94 84 89 87 89 91 
10611   THU ZECHER 83 79 89 87 88 88 86 81 84 80 89 81 
10622   BEVERLEE WAMPOLE 95 92 91 96 99 97 99 89 94 96 90 97 
10630   TRUMAN SOVIE 68 73 77 76 72 71 72 77 67 68 72 75 

You have to refer to the string type with its namespace, which is std : std::string fname .

Your example would look like this:

#include <iostream>
#include <cstdlib>
#include <string>


class Student
{
public:
    Student(int idnumber = 0, std::string fname = "", std::string lname = "");

If you want to be very pernickety, you can refer to the type as ::std::string but std::string is typically sufficient (unless you're building a general library).

you need to include std:: before hand. You can do something like using std::cout; at the top and just use cout like you have.

Finally, you need to look into overloading operators.

class Myclass {
    MyClass() { /*set defaults here*/ }
    MyClass(int id, std::string fname, std::string lname) { /* Set values here*/ }
};

use std::string

or if you don't want to write it again and again simply write

using namespace std;

in the global scope that is before main or class definition

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class Student
{
 public:
   Student(int idnumber, string fname, string lname);
class Student {
public:
    Student(int idnumber=0, std::string fname="", std::string lname="")
        : idnumber(idnumber), fname(fname), lname(lname) {}

private:
    int idnumber;
    std::string fname;
    std::string lname;
};

This uses parameter defaults to specify default values for parameters that you don't explicitly pass in. You can then construct a Student object in one of four ways:

Student s1;                      // idnumber=0, fname="", lname""
Student s2(1);                   // idnumber=1, fname="", lname=""
Student s3(1, "John");           // idnumber=1, fname="John", lname=""
Student S4(1, "John", "Smith");  // idnumber=1, fname="John", lname="Smith"

Then it uses initialization syntax to set the fields accordingly.

: idnumber(idnumber)

specifies that the class member named idnumber should be initialized with the value of the parameter idnumber . Yes, they can have the same name. The compiler knows what you mean.

The body of the constructor itself is empty because there's nothing else for it to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM