4-2
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include
<iostream> #include <string> #include <cstdio> using namespace std ; typedef struct test { int fun( void ) { return 0 ; } }StructTest; int main( void ) { StructTest test; cout<<test.fun(); system( "pause" ); } |
4-3
myhead.h
C++ Code
1
2 3 4 5 6 7 |
#ifndef
MY_HEAD_H #define MY_HEAD_H typedef struct test { int fun(); //这个函数在同名的myhead.cpp文件中定义 }StructTest; #endif |
myhead.cpp
C++ Code
1
2 3 4 |
int
test::fun(
void
)
//要像类一样定义 { return 0 ; } |
main.cpp
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include
<iostream> #include <string> #include "myhead.h" #include <cstdio> using namespace std ; int main( void ) { StructTest test; cout<<test.fun(); system( "pause" ); } |
4-4
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include
<iostream> #include <string> #include <cstdio> using namespace std ; typedef struct mydamo { int a; }Mydemo; typedef Mydemo* linkMydamon; int fun1(linkMydamon p, int a) { return a; } int fun2(linkMydamon p) { return p->a; } int main( void ) { Mydemo demo; demo.a = 100 ; linkMydamon linkdemo = &demo; cout<< "fun1========int a============" <<fun1(linkdemo,linkdemo->a)<<endl; cout<< "fun2============================" <<fun2(linkdemo)<<endl; system( "pause" ); } |
4-5 相对于4-4把全局函数改成结构体内部成员函数
4-6 this指针的使用
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include
<iostream> #include <string> #include "myhead.h" #include <cstdio> using namespace std ; class sharp { public : int a; int b; char c; sharp() { this ->inital(); this ->a = 0 ; this ->b = 0 ; } void inital() { cout<< " " ; } }; int main( void ) { sharp gfds; system( "pause" ); } |