C++编程思想 第四章数据抽象习题

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" );
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值