码迷,mamicode.com
首页 > 编程语言 > 详细

c c++混合调用

时间:2019-10-16 17:42:59      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:printf   rectangle   struct   end   LEDE   col   func   def   cal   

//extern_cpp.h

#pragma once
//extern_cpp.h

#ifdef __cplusplus
extern "C" {
#endif

int add_int(int a, int b);
char add_char(char a, char b);

typedef struct Rectangle Rectangle;
Rectangle* RectangleCreate(int width, int height);
void RectangleDestroy(Rectangle* rec);
int RectangleArea(Rectangle* rec);

#ifdef __cplusplus
}
#endif

//cppfunc.h

#pragma once

//cppfunc.h
#include "extern_cpp.h"

struct Rectangle {
    int _width;
    int _height;
    Rectangle(int width = 0, int height = 0); 
    int Area();
};


int add(int a, int b);

char add(char a, char b);

//cppfunc.cpp

//cppfunc.cpp
#include "cppfunc.h"

Rectangle::Rectangle(int width, int height):
    _width(width), _height(height)
{
}

int Rectangle::Area()
{
    return _width * _height; 
}

int add(int a, int b)
{
    return a + b;
}

char add(char a, char b)
{
    return a + b;
}

// c call

int add_int(int a, int b)
{
    return a + b;
}

char add_char(char a, char b)
{
    return a + b;
}

Rectangle* RectangleCreate(int width, int height)
{
    return new Rectangle(width, height);
}

void RectangleDestroy(Rectangle* rec)
{
    delete rec;
}

int RectangleArea(Rectangle* rec)
{
    return rec->Area();
}

//mainc.c

//mainc.c
#include <stdio.h>
#include "extern_cpp.h"

int main()
{
    Rectangle* rec = RectangleCreate(4, 12);
    int area = RectangleArea(rec);
    RectangleDestroy(rec);

    int c = add_int(12, 23);
    char d = add_char(33, 10);
    
    printf("c = %d\n", c);
    printf("d = %d\n", d);
    printf("area = %d\n", area);
    return 0;
}

//maincpp.cpp

//maincpp.cpp

#include <iostream>
#include <cstdio>

#include "cppfunc.h"

using std::cout;
using std::endl;

int main()
{
    int a = add(1000, 2000);
    char c = add(A, (char)2);
    Rectangle rec(2, 3);
    int area = rec.Area();
    cout << "a = " << a << ", c = " << c << endl;
    cout << "area = " << area << endl;
    return 0;
}

 

c c++混合调用

标签:printf   rectangle   struct   end   LEDE   col   func   def   cal   

原文地址:https://www.cnblogs.com/endenvor/p/11686945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!