C++ 理解类和构造函数

标签 c++ string class object text

我是 C++ 的新手(来自 Java)。我在用 C++ 组合类时遇到了问题。

我在这个程序中的目标是简单地实现一个带有一些字符串和计数器的基本 Animal 类。

我希望能够从我创建的文本文件中读取并将文本文件中的行设置为这些变量中的每一个。

物种 家庭 门 后代

然后我希望程序打印出所有 3 个类的结果。

我不明白如何实现默认构造函数。

这是我的类(class)。

#include <iostream>
#include <string>

using namespace std;

class Animal
{
    string species;
                string family;
                string phylum;
                string desc;
                static int count;
   public:    
       bool readIn(ifstream&file, const string frame);
    void printInfo() const;
    void setAnimal(string s, string f, string p, string d);
    static int getCount();
    Animal(string s, string f, string p, string d);
    Animal(ifstream& file, const string fname);
};    

这些是函数定义:

#include "animal.h"
#include <iostream>
#include <string>
using namespace std;

Animal::Animal(string s, string f, string p, string d)
{
        setAnimal(s,f,p,d);
}    

static int Animal::getCount()

{
    int i=0;
        i++;
        return i;
}

bool Animal::readIn(ifstream &myFile, const string fname)
{
        myFile.open(fname);
        if(myFile)
        {
                getline(myFile, species);
                getline(myFile, family);
                getline(myFile, phylum);
                getline(myFile, desc);
                myFile.close();
                return true;
        } 
        else
        return false;
}   


Animal::Animal(ifstream& file, const string fname)
{
        if(!readIn(file, fname) )
        species="ape";
        family="ape";
        phylum="ape";
        desc="ape";
        count = 1;
}   

void Animal::printInfo() const
{
cout << species << endl;
cout << family << endl;
cout << phylum << endl;
cout << desc << endl;
}

void Animal::setAnimal(string s, string f, string p, string d) 
{
        species = s, family = f, phylum = p, desc = d;
}

int main()
{
        ifstream myFile;
        Animal a;
        Animal b("homo sapien", "primate", "chordata", "erectus");
        Animal c(myFile, "horse.txt");
        a.printInfo();
        b.printInfo();
        c.printInfo();
}

最佳答案

默认构造函数是可以在不指定参数的情况下调用的构造函数。此描述可能看起来有点冗长,因此请考虑几种可能性。

通常,或者默认情况下(没有双关语),默认构造函数只是一个不带参数的构造函数:

class Animal
{
public:
  Animal() {}; // This is a default constructor
};

其他时候,虽然您可能会编写一个带参数的构造函数,但所有参数都有默认值:

class Animal
{
public:
  Animal(int age=42) : age_(age) {};  // This is a default constructor
private:
  int age_;
};

这也是一个默认构造函数,因为它可以不带参数调用:

Animal a;  // OK

您不会希望在一个类中有 2 个默认构造函数。也就是说,不要尝试编写这样的类:

class Animal
{
   public:

      Animal() {};
      Animal(int age=42) : age_(age) {}; 
    private:
      int age_;
};

在C++中,如果你有一个没有默认构造函数的类,编译器会自动为你生成一个。但是,如果您自己声明了任何 其他构造函数,编译器不会自动生成默认构造函数。所以在你的情况下,因为你已经声明了 2 个其他构造函数(都是“转换”构造函数),编译器不会为你生成默认构造函数。由于您定义的类没有默认构造函数,因此您不能默认构造 Animal 对象。换句话说,这不会编译:

Animal a;

关于C++ 理解类和构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13293658/

相关文章:

c++ - 在 C++ 中复制数组的最快可移植方法是什么

C++ getline() 分隔符

c - fopen 函数将垃圾放在文件路径名上

php - 如何只从 PHP 字符串中取出数字?

javascript - 如何从 JQuery AJAX 调用返回多个值?

Java - 从另一个类的 ArrayList 中读取

c++ - 为什么默认参数构造函数被称为默认构造函数

c++ - 为什么在死锁的情况下 lock() 不抛出异常

c++ - 尝试在类定义中声明数组时出错

php - Symfony2 类的使用