Are these terms correct in C ++?

I am reading a book, Primer's guide to C ++.

I think I got to the bottom, I just want to clarify a few things.

  • It says that classis similar to the type of data (data type A int, char, ect ..) and that Object/ Instance - as a variable. It's true?

  • What is a data form?

  • What part of the statement is this announcement? This is data type+ the variable, but =is this an appointment?

+5
source share
1 answer

Not really.

The class is "as a data type" in the sense that it is a template for creating an object, but it is not an object that you can use.

, . , - .

:

class Person
{
    public:
        Person() : name("Joe Bloggs") {}

        std::string getName() { return name; } 

        void setName(std::string n) { name = n; }

    private:
        std::string name;    
};

Person. . setName , . :

int main()
{
    Person p, q;
    p.setName("Jill Bloggs");
    q.setName("Bob King");
    std::cout << p.getName() << " " << q.getName() << std::endl;
}

, , - , .

, " ", .

- , - . , , :

class foo
{
    public:

        foo(int value);

        void bar();

    private:
        int x;
};

, , bar, void , x.

x , , . , , bar, :

void foo::bar()
{
    //some code
}

foo, x , ( ):

foo::foo(int value) : x(value)
{
    //some code
}
+5

All Articles