1 Star 0 Fork 2

codes_test/cpp-Interview2

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
struct_function.cpp 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
ericps 提交于 2022-03-24 22:06 +08:00 . upload code for struct
//
// Created by pengshuai on 2022/3/24.
//
#include<iostream>
#include<cstdio>
using namespace std;
struct Base {
int v1{};
private:
int v3{};
public: // 显示声明public
int v2{};
static void print(){
printf("%s\n","Base hello world");
};
Base() {
cout<<"Base construct"<<endl;
};
virtual ~Base() {
cout<<"Base deconstruct"<<endl;
};
};
typedef struct Base1 {
int v1;
// private: //error!
int v3;
public: //显示声明public
int v2;
static void print(){
printf("%s\n","hello world");
};
} B;
struct Derived:Base {
Derived() {
cout<<"Derived construct"<<endl;
};
~Derived() override {
cout<<"Derived deconstruct"<<endl;
};
public:
int v2 {};
static void print(){
printf("%s\n","Derived, hello world!");
};
};
//void Base(){
// printf("%s\n","I am Base func");
//}
//void B() {} //error! 符号 "B" 已经被定义为一个 "struct Base1" 的别名
int main() {
// Base base{}; // error
struct Base base{};
base.v1=1;
// base.v3=2; // error
Base::print();
printf("%d\n",base.v1);
// printf("%d\n",base.v3); // error
Base *b = new Derived();
b->print(); // todo:Base::print() 和 class 有点不一样
delete b;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/codes_test/cpp-interview2.git
git@gitee.com:codes_test/cpp-interview2.git
codes_test
cpp-interview2
cpp-Interview2
main

搜索帮助