《C++语言基础》程序阅读——指针、const、static

简介: 返回:贺老师课程教学链接本周再补充三个和指针有关的阅读程序,进一步掌握指针工作的原理。友情提醒:画出内存,让程序的分析,在理性、有序中完成。如果有时间的变化,博客中加个自己画的图。可以在上机时通过单步执行,进一步和你在人脑中运行程序的过程进行对照。(1) 阅读程序,写出程序的运行结果并理解#include <iostream>using namespace std;

返回:贺老师课程教学链接


本周再补充三个和指针有关的阅读程序,进一步掌握指针工作的原理。
友情提醒:画出内存,让程序的分析,在理性、有序中完成。如果有时间的变化,博客中加个自己画的图。
可以在上机时通过单步执行,进一步和你在人脑中运行程序的过程进行对照。

(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;
    ptr->set(50);
    base a[2]= {1,9};
    cout<<a[0].get()<<","<<a[1].get()<<endl;
    cout<<ptr->get()<<",";
    ptr=ptr-1;
    cout<<ptr->get()<<endl;
    delete[] ptr;
    return 0;
}

(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:c);
}
 
int main()
{
    int x=5,y=12,z=8;
    CE *ep;
    ep=new CE;
    ep->SetValue(x+y,y-z,10);
    cout<<ep->GetMin()<<endl;
    CE a=*ep;
    cout<<a.GetMin()*3+15<<endl;
    return 0;
}

(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;
}

(4) 请写出程序中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 = 0 ) 
    {
        name = nm;
        score = sc;
}
 
    //(2)下面的const分别干神马?___________
    const string& get_name() const
    {
        return name;   
}
 
    int get_score() const
    {
        return score;
    }
private:
    string name;
    int score;
};
 
//(3)下面的const干神马?_____________
void output_student(const Student& student ) 
{
    cout << student.get_name() << "\t";
    cout << student.get_score() << endl;
}
 
int main()
{
    Student stu( "Wang", 85 );
    output_student( stu );
    return 0;
}

(5) 请写出下面程序的输出结果
#include<iostream>
using namespace std;
class myClass
{
public:
    myClass(){ number++;}
    ~myClass(){ number--;}
    static int number;
};
 
int myClass::number=0;
 
int main()
{
    myClass *ptr;
    myClass A,B;
    myClass *ptr_Arr=new myClass[3];
    ptr=ptr_Arr;
    myClass C;
    cout<<myClass::number<<endl;
    delete []ptr;
    return 0;
}

(6) 请写出下面程序的输出结果
#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;
    cout<<"Result3="<<r.a<<endl;
}
 
int main(){
  cout <<"Resultl="<<Test::func()<<endl;
  Test a;
  cout<<"Result2="<<a.func()<<endl;
  Test::sfunc (a);
  return 0;
}


目录
相关文章
|
1天前
|
C++ 数据格式
LabVIEW传递接收C/C++DLL指针
LabVIEW传递接收C/C++DLL指针
|
1天前
|
存储 安全 程序员
C++:智能指针
C++:智能指针
19 5
|
2天前
|
存储 安全 C++
深入理解C++中的指针与引用
深入理解C++中的指针与引用
5 0
|
3天前
|
算法 C++
【C++入门到精通】智能指针 shared_ptr循环引用 | weak_ptr 简介及C++模拟实现 [ C++入门 ]
【C++入门到精通】智能指针 shared_ptr循环引用 | weak_ptr 简介及C++模拟实现 [ C++入门 ]
8 0
|
3天前
|
安全 算法 数据安全/隐私保护
【C++入门到精通】智能指针 shared_ptr 简介及C++模拟实现 [ C++入门 ]
【C++入门到精通】智能指针 shared_ptr 简介及C++模拟实现 [ C++入门 ]
6 0
|
3天前
|
存储 算法 安全
【C++入门到精通】智能指针 auto_ptr、unique_ptr简介及C++模拟实现 [ C++入门 ]
【C++入门到精通】智能指针 auto_ptr、unique_ptr简介及C++模拟实现 [ C++入门 ]
7 0
|
3天前
|
安全 算法 IDE
【C++入门到精通】智能指针 [ C++入门 ]
【C++入门到精通】智能指针 [ C++入门 ]
6 0
|
5天前
|
存储 编译器 C语言
【C++】类与对象【定义、访问限定符、this指针】
【C++】类与对象【定义、访问限定符、this指针】
5 1
|
9天前
|
C++
【C++】一文深入浅出带你参透库中的几种 [ 智能指针 ]及其背后实现原理(代码&图示)
【C++】一文深入浅出带你参透库中的几种 [ 智能指针 ]及其背后实现原理(代码&图示)
|
12天前
|
运维 Serverless Go
Serverless 应用引擎产品使用之在阿里云函数计算中c++模板,将编译好的C++程序放进去部署如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
12 1