如何实现C++中使用的成员函数来转换C中的相同程序?

Gtl*_*amr 1 c c++

我需要将一个程序从C++转换为C,即我需要相同的程序,但在C中.
我在C++中使用了成员函数,但我只知道基本的C级.
我知道这可以用结构来完成,但是怎么做我在C中实现了Structure的类函数?
这是代码.这是一个简单的程序,可以存入银行账户,存款,取款或显示账户状态:

#include<iostream>
#include<string.h>

int count = 0;

using namespace std;

class bank
{
public:
    long bal, dep, wdamt; 
    int acno; 
    char name[20], type;
public:
    void set(int x, char n[20], char t)
    {
        acno = x;
        strcpy(name, n);
        type = t;
        bal = 0;
    }
    void deposit(long z)
    {
        bal += z;
        cout << "success!";
    }
    void withdraw(long k)
    {
        bal -= k;
        cout << "success!";
    }
    void display()
    {
        cout << "\n Name : " << name << "\n AC BALANCE : " << bal;
    }
};

int main()
{
    bank cust[10];
    int c1, c2, cacno, i = 0, j = 0; 
    long cbal, cdep, cwdamt; 
    char cname[20], ctype;

    while (j < 10)
    {

        cout << "\n \n **** welcome to golu bank ****";
        cout << "\n choose one of the following \n1. New customer \n2. Existing Customer \n 3. Exit";
        cin >> c1;

        switch (c1)
        {
        case 1: cout << "\nnew customer. please enter name. ";
            cin >> cname;
            cout << "\nEnter account type. ";
            cin >> ctype;
            cust[count].set(count, cname, ctype);
            cout << "Hello! Your a/c number is " << cust[count].acno << endl << "PRESS ENTER TO CONTINUE:";
            cin.get();
            count++;
            break;
        case 2: cout << "Existing Customer. Enter account number";
            cin >> cacno;
            while (i < 10)
            {
                if (cacno == cust[i].acno)
                {
                    cout << "\n Enter choice! \n 1.Deposit \n 2. Withdraw \n 3. Display\n 4.Exit";
                    cin >> c2;
                    switch (c2)
                    {
                    case 1: cout << "Enter amount to deposit";
                        cin >> cdep;
                        cust[i].deposit(cdep); break;
                    case 2: cout << "Enter amount to withdraw";
                        cin >> cwdamt;
                        if (cwdamt > cust[i].bal)
                            cout << "Insufficent funds!!!";
                        else
                            cust[i].withdraw(cwdamt);
                        break;
                    case 3:  cust[i].display();
                        break;
                    case 4:  i = 11;
                    default: "Invalid choice";
                    }
                }
                else
                    i++;
            }
        case 3: j = 11;
        default: "Invalid choice";
        }
    }
    return 0;
    cin.get();
}
Run Code Online (Sandbox Code Playgroud)

rbf*_*rbf 8

只要您使用C++作为C++的一部分,下面的示例就可以正常工作.没有std或C++特性,在C中没有简单的等价物(例如模板).

  1. 使用结构而不是类

    struct prev_class {
        long bal,dep,wdamt; int acno; char name[20], type;
    };
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用作为第一个参数指向该结构的指针将您的成员函数转换为自由函数:

    void prev_class_set(struct prev_class* obj, int x, char n[20], char t) {
        obj->acno = x;
        strcpy(obj->name, n);
        obj->type = t;
        obj->bal = 0;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 替换喜欢你的C++特定的呼叫coutcin使用C符合功能.

  • @ machine_1我认为这根本不是一个术语. (3认同)
  • @AjayBrahmakshatriya _function vector_不是C术语,据我所知. (2认同)

Jab*_*cky 5

大概是这样的:

  • 转换所有类方法,如set正常函数(更多细节如下)
  • 将所有输入/输出转换cin/coutscanf/printf
  • 转换class bankstruct bank,删除所有private:/public:
  • this为所有转换后的类方法添加另一个参数,以便您可以模拟更多的C++ this,见下文

转换后的set函数(名称set已被替换bank_set,这不是必需的,但如果set事先已有自由函数,则可能会阻止名称冲突):

void bank_set(struct bank *this, int x, char n[20], char t)
{
    this->acno = x;
    strcpy(this->name, n);
    this->type = t;
    this->bal = 0;
}
Run Code Online (Sandbox Code Playgroud)

将调用转换为类方法,例如:

cust[count].set(count, cname, ctype);
Run Code Online (Sandbox Code Playgroud)

变为:

bank_set(&cust[count], count, cname, ctype);
Run Code Online (Sandbox Code Playgroud)

对所有其他类方法执行相同操作.

这只能起作用,因为你的C++程序是老式的"C-like"C++而且你没有使用继承.

  • 好吧,任何想成为程序员的人都必须学习OO,无论他们选择哪种语言.它是一种与语言无关的设计计算机程序的方法.某些语言比其他语言具有更多内置支持. (2认同)