zoukankan      html  css  js  c++  java
  • C++基础

    #include<iostream>
    using namespace std;
    class Point{
        public://C++中类和结构体的起到的作用类似但是类可以进行对外的访问控制
            int x;
            int y;
            /*void init(){
                x = 0;
                y = 0;
            }*/
            Point(){
                x= 0 ;
                y = 0;
            }    //构造函数(给对象赋一个初值,也就是来创建对象本身 )为了保证唯一性,采用类名,没有返回值
            Point(int a, int b){
                x = a;
                y = b;
            } //函数的重载
            /*重载构成的条件:函数的参数类型,参数个数不同*/
            /*当一个对象生命周期结束需要一个函数用来回收内存,就要用到析构函数*/
            ~Point(){
                
            }    
            void output(){
                cout<<x<<endl<<y<<endl;
            }
            void output(int x, int y){
                /*this(=&pt)指针是一个隐含的指针,它指向对象本身,代表了对象的地址
                一个类所有的对象调用的成员函数都是同一代码段。所有对数据成员的访问都
                隐含的被加上前缀this->例如x = 0,等价于this->x = 0*/
                this->x = x;
                  y = y;
            }
     };
     /*struct Point{
         int x;
         int y;
         void init(){
                x = 0;
                y = 0;
            }
            void output(){
                cout<<x<<endl<<y<<endl;
            }
         
     };*/
     int main(){
         Point pt(3,3);    //当我们声明对象后,才去调用上面的那个构造函数
         //pt.init();
         pt.output(5,5);
         //pt.x = 5;
         //cout<<pt.x<<endl<<pt.y<<endl;
         pt.output();
         return 0;
     }

    #include<iostream>
    using namespace std;
    class Animal{
        public:
            void eat(){
                cout<<"anmial eat"<<endl;
            }
        protected:
            void sleep(){
                cout<<"anmial sleep"<<endl;
            }
            void run(){
                cout<<"anmial run"<<endl;
            }
    };
    class Pig: public Animal{//类的继承
            public:
            void breathe(){
                Animal::eat();//::作用于标识符说明eat()这个函数是属于哪个类的
                cout<<"pig hunger"<<endl;
            }
    };
     int main(){
         Animal an;
         an.eat();
         Pig pg;
         pg.eat();
         
     }

  • 相关阅读:
    前端工程部署
    xcode使用入门
    effective java
    java测试用例的编写
    记录一次随意操作数据库,插入新数据,导致与程序添加新数据时,引起的主键值重复问题。More than one row with the given identifier was found: 1690
    Spring.Net中的依赖注入(DI)
    记一次使用js中的this关键字在ajax中使用,因传入的this对象不同而引起的问题。
    代理模式
    异常: 指定的转换无效
    log4net.config配置启用的几种方式
  • 原文地址:https://www.cnblogs.com/ylHe/p/5982521.html
Copyright © 2011-2022 走看看