* 指针简单总结
//接受命令行参数的函数
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 就表示当前变量.
分享到:
相关推荐
在次有大量的关于c++面向对象编程的实例 对于初级的学习者有很大的帮助
C++面向对象
C++面向对象程序设计教程(第3版)——陈维兴、林小茶编著 清华大学出版社 这个是对应的PPT课件,文件为压缩包,里面每章一个PPT文件。
c++面向对象程序设计课后习题答案c++面向对象程序设计课后习题答案c++面向对象程序设计课后习题答案
该文件是谭浩强《谭浩强 C++面向对象程序设计》电子扫描版,高清
c++面向对象考试题即答案 很有用的...............
C++面向对象程序设计习题集,主要练习面向对象思想。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
C++面向对象程序设计上机考试题库.pdf
c++面向对象程序设计语言重点难点复习题及答案。
C++面向对象程序设计课后答案(清华大学出版社)
(麻省理工免费课程)C语言内存管理和C++面向对象编程,学习的好资料
课程名称: C++ 面向对象程序设计 目的:崭新的二十一世纪,以现代电子信息产业为龙头的全球经济一体化浪潮正席卷世界,这是当今人类所面临的巨大挑战,人们将认真面对挑战的内涵和挑战所带来的机遇。而以IT ...
c++面向对象程序设计第三版谭浩强教学大纲.pdf
C++面向对象高级编程代码资源及课件.tar.gz
C++面向对象高效编程C++面向对象高效编程C++面向对象高效编程C++面向对象高效编程
c++面向对象程序设计_杜茂康_课后答案[1-6章]_
期末课程设计题目,使用C++面向对象思想实现的ATM取款机的基本操作,本人也是学生,初学,写的不好,仅供参考。
清华大学出版社,龚晓庆付丽娜等编写的《C++面向对象程序设计》第二版,课后编程题参考答案。
此为清华大学出版社陈维兴等人编写的《C++面向对象程序设计教程》习题答案,仅供学习参考