【问题标题】:change this c program to c++(structure to class)将此c程序更改为c ++(结构到类)
【发布时间】:2022-06-23 00:22:48
【问题描述】:

我制作了这个程序并将 c 和 c++ 混合在一起,任何人都可以帮助我用 c++ 制作这个程序。 专门把这个结构做成类。 //这只是一个评论,因为我无法发布这个问题,所以请忽略斜线之间的任何内容//

using namespace std;

struct student
{
    char name[50];
    int roll;
    float marks;
} s[10];

int main()
{
    char ch,choice,disp;
    int i=0,c=0;
    cout<<"Do you want to\n1. Store details of students.\n2. Display the details of students."<<endl;
    cin>>ch;
    switch (ch)
    {
    case '1': 
        cout << "Enter information of students: " << endl;
        // storing information
        do{
            s[i].roll = i+1;
            cout << "For roll number : " << s[i].roll << endl;

            cout << "Enter name: ";
            cin >> s[i].name;

            cout << "Enter marks: ";
            cin >> s[i].marks;

            cout << endl;
            i++;
            c++;
            cout<<"Do you want to add one more entry ?(y/n)"<<endl;
            cin>>choice;
        }while(choice == 'y' || choice == 'Y');
        cout<<"Want to display all the students info ?(y/n)";
        cin>>disp;
        if(disp=='n' || disp=='N')
        {
            break;
        }
    case '2': 
        cout << "Displaying Information: " << endl;
        // Displaying information
        for(int i = 0; i < c; ++i)
            {
            cout << "\nRoll number: " << i+1 << endl;
            cout << "Name: " << s[i].name << endl;
            cout << "Marks: " << s[i].marks << endl;
            }
        break;
    default: 
            cout<<"The page is in development please choose valid choices";
        }
    return 0;
}```

【问题讨论】:

  • C++ 中structclass 的唯一区别是struct 成员默认为public,而class 成员默认为private。所以把struct改成class,在成员前加上public:

标签: c++ c loops class structure


【解决方案1】:

如果你添加这一行,它在 C++ 中编译得很好

#include <iostream>

在代码的顶部。

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多