第五周 程序阅读——指针(3)

#include <iostream>
using namespace std;
class Time
{
public:
    Time(int,int,int);
    void output_time( );
    int hour;
    int minute;
    int sec;
};

Time::Time(int h,int m,int s)
{
    hour=h;
    minute=m;
    sec=s;
}

void Time::output_time( )
{
    cout<<hour<<":";
    cout<<minute<<":" <<sec<<endl;
}

int main( )
{
    Time t1(10,13,56);
    int *p1=&t1.hour; //指向数据成员的指针
    cout<<*p1<<endl;
    t1.output_time( );

    Time *p2=&t1; //指向对象的指针
    p2->output_time( );

    void (Time::*p3)( ); //指向成员函数的指针
    p3=&Time::output_time;
    (t1.*p3)( );
    return 0;
}

运行结果:

知识点总结:

void (Time::*p3)( ); //指向成员函数的指针

p3=&Time::output_time;

(t1.*p3)( );

学习心得:

好好学习 天天向上

时间: 2024-07-29 22:21:03

第五周 程序阅读——指针(3)的相关文章

第五周 程序阅读——指针(2)

#include<iostream> using namespace std; class CE { private: int a,b; int getmin(){return (a<b? a:b);} public: int c; void SetValue(int x1,int x2, int x3) { a=x1; b=x2; c=x3; } int GetMin(); }; int CE::GetMin() { int d=getmin(); return (d<c? d:

第五周 程序阅读——指针(1)

#include <iostream> using namespace std; class base { private: int m; public: base() {}; base(int m){this->m=m;} int get(){return m;} void set(int m){this->m=m;} };//base_end int main() { base *ptr; ptr=new base[2]; ptr->set(30); ptr=ptr+1;

第五周 程序阅读——const

#include <iostream> #include <string> using namespace std; class Student { public: Student() {} Student( const string& nm, int sc = 0 ): name(nm), score(sc){} //(1)下面的const干神马?_____________ void set_student( const string& nm, int sc =

第五周 程序阅读——static(2)

#include <iostream> using namespace std; class Test{ private: static int val; int a; public: static int func(); static void sfunc(Test &r); }; int Test::val=20; int Test::func() { val+=val; return val; } void Test::sfunc (Test &r) { r.a=25;

第九周 程序阅读-字符串类的设计

阅读下面的程序,领会其中用到的设计方案.技术手段与算法. /* 对于要定义的字符串类CMyString, 数据成员包括: - 字符串的长度: - 指向字符串第一个字符的指针 成员函数包括: - 不带参数的构造函数: - 带一个类型为const char *类型的参数(用于对字符串初始化)的构造函数: - 带一个const CMyString&类型的复制构造参数: - 析构函数: - Strlen函数 (用于求字符串的长度): - int Find(char c) (找出字符c在本字符串中第一次出

第九周-程序阅读理解

/*Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:my.cpp *作 者:张瀚文 *完成日期:2016年5月6日 * *问题描述: 阅读程序,写出程序的运行结果并理解其运行机制. */ #include <iostream> #include<cstring> using namespace std; class A { char *a; public: A(char *aa) { a=new char[s

第十二周程序阅读5:基类和派生类的转换

问题及代码: #include <iostream> using namespace std; class A { protected: int a,b; public: A(int aa, int bb):a(aa), b(bb) {} void printA() { cout<<"a: "<<a<<"\tb: "<<b<<endl; } }; class B: public A { int

第15周 程序阅读-二进制文件及文件的读取1

1.阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法).查看其内容,并理解文件存储的原理. (1) #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a; ofstream outfile("f1.dat",io

第15周 程序阅读-二进制及二进制文件的读取3

3.阅读下面的程序,指出其功能,体会seekg().tellg()等函数的功能及其用法 (1) #include<iostream> #include <fstream> using namespace std; const char * filename = "a.txt"; int main () { long l,m; ifstream file (filename, ios::in|ios::binary); l = file.tellg(); file