C++ 面向对象

* 指针简单总结
//接受命令行参数的函数
int main( int argc/*字符串的个数,>=1*/, char * argv[]/*指向字符串*/ )
{
//参数的个数:argc-1
//参数:argv[1~n]
cout << sizeof("goodbye");//输出8
}


引用:给旧变量起新名字,通过新名字操作旧变量.
引用和用来初始化它的变量是同一个实体.
引用不会复制数据,提高了效率.
传数组一般不用引用.直接传数组名
sizeof(类型)//只关心类型,不关心内容
//示例代码
int x=0;
cout << sizeof(1/x) << endl;//输出4

int (&p)[10] //数组引用,没必要这样写,建议不使用,注意数组名不是变量
//没必要为其起别名
(int * p, int n ) //这样写比较好

函数指针:像声明函数一样定义函数指针,把函数名改成(*指针),占4个字节,因为它是一个指针.
函数调用:fp( /* args */ )

void 指针把类型信息给丢了.
没有类型的指针不能用来表示变量,所以用void指针时一定要强转 *(A*)p
指针有两点:1,保存地址 2,类型一致

-----------------------------------------------------------

*** 面向对象编程

//时钟程序
//clock.h
#define _CLOCK_H_

struct Time{
int hour;
int minute;
int second;
};

void set( Time* p, int h, int m, int s );
void tick( Time* p );
void show( Time* p );
void run( Time* p );

#endif


//clock.cc
#include "clock.h"
#include <ctime>
#include <iostream>
using namespace std;

void set( Time* p, int h, int m, int s )
{
p->hour = h;
p->minute = m;
p->second = s;
}

void tick( Time* p )
{
long t = time(NULL);
while( t == time(NULL) );
if( ++p->second >=60 ){
p->second = 0;
if( ++p->minute >= 60 ){
p->minute = 0;
if( ++p->hour >=24 ){
p->hour = 0;
}
}
}
}
void show( Time* p )
{
cout << '\r';
if( p->hour < 10 ) cout << 0;
cout << p->hour << ':';
if( p->minute <10 ) cout << 0;
cout << p->minute << ':';
if( p->second <10 ) cout << 0;
cout << p->second << flush;
}
void run( Time* p )
{
for(;;){
tick( p );
show( p );
}
}

//main.cc
#include "clock.h"
using namespace std;

int main()
{
Time t;
set( &t,21,35,55 );
run( &t );
}



//时钟程序 改进型
//main.cc
#include "clk.h"
using namespace std;

int main()
{
Time t;
t.set( 21,35,55 );
t.run( );
}
//这个地方用了VI一条替换的命令,将上面的程序中的所有的p替换成this
//1,$s/p/this/g

//clk.h
#ifndef _CLOCK_H_
#define _CLOCK_H_

struct Time{
int hour;
int minute;
int second;

void set( int h, int m, int s );
void tick( );
void show( );
void run( );
};

#endif

//clk.cc
#include "clk.h"
#include <ctime>
#include <iostream>
using namespace std;

void Time::set( int h, int m, int s )
{
this->hour = h;//this可省略不写
this->minute = m;
this->second = s;
}

void Time::tick( )
{
long t = time(NULL);
while( t == time(NULL) );
if( ++this->second >=60 ){
this->second = 0;
if( ++this->minute >= 60 ){
this->minute = 0;
if( ++this->hour >=24 ){
this->hour = 0;
}
}
}
}
void Time::show( )
{
cout << '\r';
if( this->hour < 10 ) cout << 0;
cout << this->hour << ':';
if( this->minute <10 ) cout << 0;
cout << this->minute << ':';
if( this->second <10 ) cout << 0;
cout << this->second << flush;
}
void Time::run( )
{
for(;;){
tick( );
show( );
}
}


//clk.h改进成类
#ifndef _CLOCK_H_
#define _CLOCK_H_

class Time{
int hour;
int minute;
int second;

public:
void set( int h, int m, int s );
void tick( );
void show( );
void run( );
};

#endif


在成员函数中,把用来调用这成员函数的变量称为当前变量(this总是指向当前变量)
*this 就表示当前变量.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值