[侯捷_C++面向对象高级开发_上] 2 头文件和类的声明

发布时间 2023-10-23 17:22:49作者: StarAire

1. C vs C++ 关于数据和函数

  • C
    • Data
    • Functions
    • 对于C来说,数据大部分情况是所有函数都可以访问的,这样对程序来说会变得很混乱
  • C++
    • Data Members
    • Member Functions
    • 对于C++来说,数据和函数封装在一起形成类,可以设定为数据只能让类里的函数访问,具有良好的组织性

2. C++ 关于数据和函数

  • 不带指针 Complex
    • 成员
      • 成员变量:实部、虚部
      • 成员函数:加、减、乘、除、共轭、正弦...
    • 初始方式
      • complex c1(2,1);
      • complex c2;
      • complex *pc = new complex(0,1);
  • 带指针 String
    • 成员
      • 成员变量:字符(s):其实是个ptr,指向一串字符
      • 成员函数:拷贝、输出、附加、插入
    • 初始方式
      • string s1("hello ");
      • string s2("world ");
      • string *ps = new string;

3. Object Based 基于对象 vs Object Oriented 面向对象

  • Object Based:面对的是单一class的设计
  • Object Oriented:面对的多重classes的设计,classes和classes之间的关系

4. C++ programs代码基本形式

ex.main()
Classed
Declaration
(声明)
Standard
Library
(标准库)
 #include <iostream.h>
.h (header files)
.cpp
.h (header files)
 #include <iostream.h>

延伸文件名(extension file name)不一定是.h或者.cpp
也可能是.hpp或者其他或者甚至无延伸名

5. Output C++ vs C

  • C++
    #include <iostream.h> // #include <iostream>
    using namespace std;
    int main()
    {
        int i =7;
        cout << "i=" << i << endl;
        return 0;
    } 
    
  • C
    #include <stdio.h> // #include <cstdio>
    
    int main()
    {
        int i = 7;
        printf("i=%d\n",i);
        return 0;
    }
    

6. Header(头文件)的防重复声明

  • complex.h

    #ifndef __COMPLEX__
    #define __COMPLEX__
    
    ...
    
    #endif
    
  • complex-test.cpp

    #include <iostream>
    #include "complex.h"
    using namespace std;
    
    int main()
    {
        complex c1(2,1);
        complex c2;
        cout << c1 << endl;
        cout << c2 << endl;
        
        c2 = c1 + 5;
        c2 = 7 + c1;
        c2 = c1 + c2;
        c2 += c1;
        c2 += 3;
        c2 = -c1;
            
        cout << (c1 == c2) << endl;
        cout << (c1 != c2) << endl;
        cout << conj(c1) <<endl;
        return 0;
    }
    
    

7. 头文件布局 class的声明

     #ifndef COMPLEX
     #define COMPLEX
  #include <cmath.h>

  class ostram;
  calss complex;
  
  complex &__doap1(complex this,const complex &r);
forward declarations
(前置声明)
  class complex
  {
   ...
   };

  complex::function ...
#endif
class declarations
(类-声明)
class definition
(类-定义)
 class complex
 {
 public:
complex (double r = 0,double i = 0)
: re (r) , im (i) 
{  }
complex & operator +=  (const comlex&);
double real() const {return re;}
double imag() const {return im;}
 private:
double re,im;
friend complex& __doap1 (complex
,const complex&);
 };
有些函数在此直接定义,另一些在body之外定义
{
complex c1(2,1);
complex c2;
...
}
class的声明(declaration)

8. class template 模板 简介

 class complex
 {
 public:
complex (double r = 0,double i = 0)
: re (r) , im (i) 
{  }
complex & operator +=  (const comlex&);
double real() const {return re;}
double imag() const {return im;}
 private:
double re,im;
friend complex& __doap1 (complex,const complex&);
 };
有些函数在此直接定义,另一些在body之外定义
class的声明(declaration)
 template<typename T> 
 class complex
 {
 public:
complex (T r = 0,T i = 0)
: re (r) , im (i) 
{  }
complex & operator +=  (const comlex&);
T real() const {return re;}
T imag() const {return im;}
 private:
T re,im;
friend complex& __doap1 (complex
,const complex&);
 };
 {
complex c1(2,1);
complex c2;
...
 }
 {
complex<double> c1(2.5,1.5);
complex<int> c2(2,6);
...
 }
模板化