面向对象程序设计(一):什么是类

C++类的概念:

下面我们介绍一个类的实际例程:

1.定义一个类,描述一个点的坐标

2.定义一个派生类,描述一个圆

#include<iostream>
using namespace std;

class point{
    
    protected:
        int x;
        int y;
        
    public:
        void setPoint(){
            cout<<"请输入两个坐标值 中间用空格隔开:"<<endl; 
            cin>>x>>y;
        }
        void movePoint(int dx,int dy){
            cout<<"请输入dx dy,中间用空格隔开:"<<endl; 
            cin>>dx>>dy;
            x+=dx;
            y+=dy;
        }
        void printPoint(){
            cout<<"最终的坐标为:"<<endl; 
            cout<<x<<','<<y<<endl;
        }
        
};

class circle:public point{
    protected:
        int R;
    public:
        void setR(){
            cout<<"请输入圆的半径值:"<<endl; 
            cin>>R;
        }
        void printCircle(){
            cout<<"圆心为"<<x<<','<<y<<"半径为"<<R<<endl;
        }
        
};

int main(){
    
    point A;
    A.setPoint();
    A.movePoint(0,0);
    A.printPoint();
    
    circle B;
    B.setPoint();
    B.setR();
    B.movePoint(0,0);
    B.printCircle();
    
    return 0;
}

 

程序运行结果如下:

 ps:可以右键在新标签页中查看大图

 

第一次使用类呢,到这里就结束了

笔者刚刚开始学习计算机,还有很多地方不懂,我们先挖个坑,等到时候在填上!!( ̄︶ ̄)↗ 

posted @ 2020-11-16 12:48  dou_fu_gan  阅读(310)  评论(0)    收藏  举报