【问题标题】:I can't call my class in to main function我无法将我的类调用到主函数中
【发布时间】:2014-09-01 14:00:11
【问题描述】:
#include<iostream>  
using namespace std;

class student 
   {

        private:
            char name [30], location[50], ic[20];
            int age;

        public:
            void set_data()
            {
                cout<<"Enter name       :";
                cin.getline(name, 30);
                cout<<"Enter location       :";
                cin.getline(location, 50);
                cout<<"Enter IC     :";
                cin.getline(ic, 20);
                cout<<"Enter age        :";
                cin>>age;
            }

            void display()
           {
               cout<<"Name       :"<<name<<endl;
               cout<<"IC         :"<<ic<<endl;
               cout<<"Location   :"<<location<<endl;
               cout<<"Age        :"<<age<<endl;
           }
    }student s1;

   int main()
{   

    cout<<"--------------------------------"<<endl;
    cout<<"WELCOME TO A-PLUS TUITION CENTER"<<endl;
    cout<<"--------------------------------"<<endl;

    student set_data();


    cout<<"--------------------------------"<<endl;
    cout<<"STUDENT INFORMATION"<<endl;
    cout<<"--------------------------------"<<endl;

    student display();

return 0;
}

各位请帮帮我...我被困在这里..我需要这样的输出,但是我在将类调用到主函数时遇到问题..我该怎么办?

--------------------------------
WELCOME TO A-PLUS TUITION CENTER
--------------------------------
Enter Name     : Usha Vellappan
Enter Location : Melaka
Enter IC       : 750217016680
Enter age      : 30

--------------------------------
STUDENT INFOMATION
--------------------------------
Name      : Usha Vellappan
IC        : 750217016680
Location  : Melaka
Age       : 30


Press any key to continue

【问题讨论】:

  • 有什么问题?它似乎正在工作。
  • 初读 OOP 概念。它只是关于创建类的实例并通过这些实例调用方法。先自己试一试
  • 我强烈建议你买一本 C++ 书籍并开始阅读。
  • 我现在的错误是它的“'s1'之前的预期初始化程序......那么我该如何更改我的代码?

标签: c++ function class object main


【解决方案1】:

考虑到这个例子,你做错了什么......

我修复了代码

#include<iostream>  

使用命名空间标准;

班级学生 {

    private:
        char name [30], location[50], ic[20];
        int age;

    public:
        void set_data()
        {
            cout<<"Enter name       :";
            cin.getline(name, 30);
            cout<<"Enter location       :";
            cin.getline(location, 50);
            cout<<"Enter IC     :";
            cin.getline(ic, 20);
            cout<<"Enter age        :";
            cin>>age;
        }

        void display()
       {
           cout<<"Name       :"<<name<<endl;
           cout<<"IC         :"<<ic<<endl;
           cout<<"Location   :"<<location<<endl;
           cout<<"Age        :"<<age<<endl;
       }
}s1;

int main() {

cout<<"--------------------------------"<<endl;
cout<<"WELCOME TO A-PLUS TUITION CENTER"<<endl;
cout<<"--------------------------------"<<endl;

s1.set_data();


cout<<"--------------------------------"<<endl;
cout<<"STUDENT INFORMATION"<<endl;
cout<<"--------------------------------"<<endl;

s1.display();

返回 0; }

但我仍然会推荐你阅读一些关于 c++ 的书..

【讨论】:

  • 它确实有效 :D 非常感谢.. 我刚开始 c++ 还不到一个月,我从我的讲师那里得到了这项工作.. 我肯定会阅读更多关于 c++ 的信息
  • 这很酷,..:D 学习.. 尽可能多地学习.. 它是一门很棒的语言... 最好的...
  • 让我们说如果我必须在主函数中插入循环结构..我应该在哪里插入它?例如for循环
  • 在第一组 cout 语句之后.. 就在 s1.set_data() 之前;
【解决方案2】:

我修改了你的代码。它按预期工作。

#include<iostream>  

using namespace std;

class student {

        private:
            char name [30], location[50], ic[20];
            int age;

        public:
            void set_data()
            {
                cout << "Enter name       : ";
                cin.getline(name, 30);
                cout<<  "Enter location   : ";
                cin.getline(location, 50);
                cout<<  "Enter IC         : ";
                cin.getline(ic, 20);
                cout<<  "Enter age        : ";
                cin>>age;

            }

            void display()
           {
               cout<<"Name       : "<<name<<endl;
               cout<<"IC         : "<<ic<<endl;
               cout<<"Location   : "<<location<<endl;
               cout<<"Age        : "<<age<<endl;
           }
};

int main()
{   

    cout<<"--------------------------------"<<endl;
    cout<<"WELCOME TO A-PLUS TUITION CENTER"<<endl;
    cout<<"--------------------------------"<<endl;

    student s;
    s.set_data();


    cout<<"--------------------------------"<<endl;
    cout<<"STUDENT INFORMATION"<<endl;
    cout<<"--------------------------------"<<endl;

    s.display();

    return 0;
}

【讨论】:

    猜你喜欢
    • 2012-11-19
    • 2016-08-15
    • 2016-12-28
    • 2016-11-23
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多