zoukankan      html  css  js  c++  java
  • C++基本概念复习

    照着https://www.w3cschool.cn/cpp/,把C++的基础概念顺了一遍,虽然很久没用C++,还是整理一下。

    #include "stdafx.h";
    #include <iostream>//定义了一些头文件,这些头文件包含了程序中必需的或有用的信息,这里是包含了头文件 <iostream>
    #include <fstream>
    #include <cstring>
    #include <string>
    #include <ctime>
    
    using namespace std;//告诉编译器使用 std 命名空间。命名空间是 C++ 中一个相对新的概念。
    
    class Box
    {
        double privatevalue;//默认私有变量,或者用private声明
    public:
        double length;   // Length of a box
        double breadth;  // Breadth of a box
        double height;   // Height of a box
        /*类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。
        尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。
        友元可以是一个函数,该函数被称为友元函数;
        友元也可以是一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友元。
        声明类 ClassTwo 的所有成员函数作为类 ClassOne 的友元,需要在类 ClassOne 的定义中放置如下声明:
        friend class ClassTwo;
        如果要声明函数为一个类的友元,需要在类定义中该函数原型前使用关键字 friend*/
        friend void printLength( Box box );//友元函数
        //Box();//构造函数
        //double getVolume(void);//此定义可以在类定义的外部进行函数实现;
        void setLength( double len ){
            length = len;
        }
    
        double getVolume(void)// 返回体积
        {
            return length * breadth * height;
        }
    
        Box()//构造函数
        {
            cout << "Box class is being created" << endl;
        }
        ~Box()//析构函数
        {
            cout << "Object is being deleted" << endl;
        }
    };
    
    // 基类
    class Shape 
    {
    public:
        void setWidth(int w)
        {
            width = w;
        }
        void setHeight(int h)
        {
            height = h;
        }
    protected:
        int width;
        int height;
    };
    
    // 派生类
    class Rectangle: public Shape  //class <派生类名>:<继承方式><基类名>
    {
    public:
        int getArea()
        { 
            return (width * height); 
        }
    };
    
    //内联函数
    inline int Max(int x, int y)
    {
        return (x > y)? x : y;
    }
    void getSeconds(unsigned long *par);
    double getAverage(int *arr, int size);
    int * getRandom();
    double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};
    
    double& setValues( int i )
    {
        return vals[i];   // 返回第 i 个元素的引用
    }
    void swap(int& x, int& y);
    
    int main()
    {
        //cout << "Hello World
    "; // 输出 Hello World
    
        ////C风格字符串 include <cstring>
        //cout << "C风格字符串 include <cstring>
    ";
        //char greeting[6] = {'H', 'e', 'l', 'l', 'o', ''};
        //cout << "C风格字符串(char数组): ";
        //cout << greeting << endl;
    
    
        ////C++风格字符串 include <string>
        //cout << "C++风格字符串 include <string>
    ";
        //string str1 = "Hello";
        //string str2 = "World";
        //string str3;
        //int  len ;
        //// 复制 str1 到 str3
        //str3 = str1;
        //cout << "str3 : " << str3 << endl;
        //// 连接 str1 和 str2
        //str3 = str1 + str2;
        //cout << "str1 + str2 : " << str3 << endl;
        //// 连接后,str3 的总长度
        //len = str3.size();
        //cout << "str3.size() :  " << len << endl;
    
    
        ////C++变量地址
        //cout << "C++变量地址
    ";
        //int  var1;
        //char var2[10];
        //cout << "var1 变量的地址: ";
        //cout << &var1 << endl;
        //cout << "var2 变量的地址: ";
        //cout << &var2 << endl;
    
    
        ////C++指针
        ////指针存储的是指向的地址,*指针:代表指向变量的值;
        //cout << "C++指针
    ";
        //int  var = 20;   // 实际变量的声明
        //int  *ip;        // 指针变量的声明
        //ip = &var;       // 在指针变量中存储 var 的地址
        //cout << "Value of var variable: ";
        //cout << var << endl;
        //// 输出在指针变量中存储的地址
        //cout << "Address stored in ip variable: ";
        //cout << ip << endl;
        //// 访问指针中地址的值
        //cout << "Value of *ip variable: ";
        //cout << *ip << endl;
    
    
        ////C++传递指针给函数
        //cout << "C++传递指针给函数
    ";
        //unsigned long sec; //实际存储时间数值的变量
        //getSeconds( &sec );//&sec时间数值的地址
        //cout << "Number of seconds :" << sec << endl;
    
    
        ////函数接受数组作为参数
        //cout << "函数接受数组作为参数
    ";
        ////带有5个元素的整型数组
        //int balance[5] = {1000, 2, 3, 17, 50};
        //double avg;
        //// 传递一个指向数组的指针作为参数,balance数组名代表数组的指针?
        //avg = getAverage(balance, 5) ;
        //// 输出返回值
        //cout << "Average value is: " << avg << endl; 
    
    
        ////C++从函数返回指针
        //cout << "C++从函数返回指针
    ";
        ////一个指向整数的指针
        //int *p;
        //p = getRandom();
        //for ( int i = 0; i < 10; i++ )
        //{
        //    cout << "*(p + " << i << ") : ";
        //    cout << *(p + i)<<"-"<<p[i] << endl;//*(p+i)=p[i]
        //}
    
        /*
        ===C++ 引用===
        引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。
        一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。
        ===C++ 引用 vs 指针===
        引用很容易与指针混淆,它们之间有三个主要的不同:
        不存在空引用。引用必须连接到一块合法的内存。
        一旦引用被初始化为一个对象,就不能被指向到另一个对象。指针可以在任何时候指向到另一个对象。
        引用必须在创建时被初始化。指针可以在任何时间被初始化。
        */
    
        ////C++引用示例
        //cout << "C++引用示例
    ";
        //// 声明简单的变量
        //int    i;
        //double d;
        //// 声明引用变量
        //int&    r = i;
        //double& s = d;
        //i = 5;
        //cout << "Value of i : " << i << endl;
        //cout << "Value of i reference : " << r  << endl;
    
        //d = 11.7;
        //cout << "Value of d : " << d << endl;
        //cout << "Value of d reference : " << s  << endl;
    
    
        ////C++ 把引用作为返回值
        //cout << "C++ 把引用作为返回值
    ";
        //cout << "改变前的值" << endl;
        //for ( int i = 0; i < 5; i++ )
        //{
        //    cout << "vals[" << i << "] = ";
        //    cout << vals[i] << endl;
        //}
    
        //setValues(1) = 20.23; // 返回的是引用,改变第 2 个元素
        //setValues(3) = 70.8;  // 返回的是引用,改变第 4 个元素
    
        //cout << "改变后的值" << endl;
        //for ( int i = 0; i < 5; i++ )
        //{
        //    cout << "vals[" << i << "] = ";
        //    cout << vals[i] << endl;
        //}
    
    
        ////C++ 把引用作为参数
        //cout<< "C++ 把引用作为参数
    ";
        ////局部变量声明
        //int a = 100;
        //int b = 200;
        //cout << "交换前,a 的值:" << a << endl;
        //cout << "交换前,b 的值:" << b << endl;
        ///* 调用函数来交换值 */
        //swap(a, b);
    
        //cout << "交换后,a 的值:" << a << endl;
        //cout << "交换前,b 的值:" << b << endl;
    
    
        ////C++ 类定义
        //Box Box1;          // 声明 Box1,类型为 Box
        //Box Box2;          // 声明 Box2,类型为 Box
        //double volume = 0.0;     // 用于存储体积
    
        ////box 1 详述
        //Box1.height = 5.0; 
        //Box1.length = 6.0; 
        //Box1.breadth = 7.0;
    
        ////box 2 详述
        //Box2.height = 10.0;
        //Box2.length = 12.0;
        //Box2.breadth = 13.0;
    
        ////box 1 的体积
        //volume = Box1.height * Box1.length * Box1.breadth;
        //cout << "Box1 的体积:" << volume <<endl;
    
        ////box 2 的体积
        //volume = Box2.height * Box2.length * Box2.breadth;
        //cout << "Box2 的体积:" << volume <<endl;
    
    
        ////C++的继承
        //cout<< "C++的继承
    ";
        //Rectangle Rect;
        //Rect.setWidth(5);
        //Rect.setHeight(7);
        ////输出对象的面积
        //cout << "Total area: " << Rect.getArea() << endl;
    
        ////C++的内联函数
        ////cout << "C++的内联函数
    ";
        //cout << "Max (20,10): " << Max(20,10) << endl;
        //cout << "Max (0,200): " << Max(0,200) << endl;
        //cout << "Max (100,1010): " << Max(100,1010) << endl;
    
    
    
        ////C++的友元函数
        ////cout << "C++的友元函数
    ";
        //Box box;
        ////使用成员函数设置宽度
        //box.setLength(10.0);
        ////使用友元函数输出宽度
        //printLength( box );
    
    
    
        /*
        ===虚函数===
        虚函数 是在基类中使用关键字 virtual 声明的函数。在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数(不的调用基类的方法,而是动态子类的方法实现)。
        我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被称为动态链接,或后期绑定。
    
        ===纯虚函数===
        您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象,
        但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。
        virtual int area() = 0;
        */
    
    
    
    
    
        //控制台输入
        int in;
        cin>>in;
        return 0;
    } 
    
    //请注意:printWidth() 不是任何类的成员函数
    void printLength( Box box )
    {
        /* 因为 printWidth() 是 Box 的友元,它可以直接访问该类的任何成员 */
        cout << "Width of box : " << box.length <<endl;
    }
    
    void getSeconds(unsigned long *par)
    {
        // 获取当前的秒数
        *par = time( NULL );//*par par是时间数值的地址,*par是代表时间数值的实际值
        return;
    }
    
    double getAverage(int *arr, int size)
    {
        int    i, sum = 0;       
        double avg;          
    
        for (i = 0; i < size; ++i)
        {
            sum += arr[i];
        }
    
        avg = double(sum) / size;
    
        return avg;
    }
    
    //要生成和返回随机数的函数
    int * getRandom()
    {
        static int r[10];
        //设置种子
        srand((unsigned)time(NULL));
        for (int i = 0; i < 10; ++i)
        {
            r[i] = rand();
            cout << r[i] << endl;
        }
        return r;
    }
    
    void swap(int& x, int& y)
    {
        int temp;
        temp = x; /* 保存地址 x 的值 */
        x = y;    /* 把 y 赋值给 x */
        y = temp; /* 把 x 赋值给 y  */
    
        return;
    }
  • 相关阅读:
    幸存者偏差Survivorship Bias
    如何用一月6RMB搭建一个国外服务器
    因果性≠相关性
    三维组态可视化解决方案
    君子生非异也,善假于物也
    机器人制证系统大屏可视化
    C# WPF 嵌入网页版WebGL油田三维可视化监控
    OffscreenCanvas-离屏canvas使用说明
    去掉图片黑背景输出为透明背景
    高清屏下canvas重置尺寸引发的问题
  • 原文地址:https://www.cnblogs.com/zhaiyf/p/8243230.html
Copyright © 2011-2022 走看看