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

标签 c++ c

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

#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();
}

最佳答案

只要您使用 C++ 中类似 C 的部分,下面的示例就可以工作。没有标准或 C++ 功能,在 C 中没有简单的等效功能(例如模板)。

  1. 使用结构而不是类

    struct prev_class {
        long bal,dep,wdamt; int acno; char name[20], type;
    };
    
  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;
    }
    
  3. 将您的 C++ 特定调用(如 coutcin)替换为符合 C 的函数。

关于c++ - 如何实现C++中使用的成员函数来转换C中的同一个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49032563/

相关文章:

c++ - 单个文件非验证 xml 解析器/阅读器

c - 在 C 中将结构体的成员指向结构体本身

c - Redis源码,zmalloc.c中的(size&(sizeof(long)-1))

c++ - 使用抽象类的继承和多态

c++ - 基于文本的游戏 - std::string 来自新手的一些问题

C++ - 交叉谱密度中的复值计算错误

c - 实现 Malloc : case for splitting a block

c - 从方法中将新值设置为 C 中的二维数组

c - 标准库 include 应该写在哪里? .c 还是 .h 文件?

c++ - 什么是 (void*) casting?