C++--day07

目录: 

1. C的提高 1-131P 时间七天 
2. C++的基础 132-286P 时间八天 
3. C++的提高 287-378P 时间五天 
4. C/C++的数据结构 379-482P 时间五天 
5. C/C++的设计模式基础 483-540P 时间三天

视频资料:https://www.bilibili.com/video/av27904891?from=search&seid=10891514449061956870



P200  const修饰的谁

#include <iostream>
using namespace std;

class Test
{
public:
    Test(int a, int b) //---> Test(Test *this, int a, int b)
    {
        this->a = a;//this就是t1取地址,谁调用它它就是谁
        this->b = b;    
    }
    void printT()
    {
        cout<<"a: " <<a <<endl;
        cout<< "b: " << this->b <<endl;
    }
    //1. const写在什么位置 没有关系
    //2. const修饰的是谁
    //   1. const修饰的是形参a  不是
    //   2. const修饰的是属性this->a this->b
    //   3. const修饰的是this指针所指向的内存空间,指针所指向的内存空间不能被修改
    void OpVar(int a,int b) const // void OpVar(const Test *const this, int a, int b) 
    {
        a=100;
        //this->a=100;
        this->b=100;
    
        cout<< "b: " << this->b <<endl;
    }
protected:
private:
    int a;
    int b;
};

void main()
{

    Test t1(1, 2);
    t1.printT();// ===> printT(&t1)
    cout<<"hello..."<<endl;
    system("pause");
    return ;
}

P202  全局函数PK成员函数

1、把全局函数转化成成员函数,通过this指针隐藏左操作数

  Test add(Test &t1, Test &t2)===》Test add( Test &t2)

2、把成员函数转换成全局函数,多了一个参数

  void printAB()===》void printAB(Test *pthis)

3、函数返回元素和返回引用

Test& add(Test &t2) //*this //函数返回引用

{

this->a = this->a + t2.getA();

this->b = this->b + t2.getB();

return *this; //*操作让this指针回到元素状态

} 

 

Test add2(Test &t2) //*this //函数返回元素

{

//t3是局部变量

Test t3(this->a+t2.getA(), this->b + t2.getB()) ;

return t3;

}

 

void add3(Test &t2) //*this //函数返回元素

{

//t3是局部变量

Test t3(this->a+t2.getA(), this->b + t2.getB()) ;

//return t3;

}
#include<iostream>
using namespace std;

class Test
{
public:
    int a;
    int b;
public:
    ~Test()
    {
        cout<<"a:"<<a<<"b:"<<b;
        cout<<"析构函数自动调用"<<endl;
    }
public:
    Test(int a=0,int b=0)
    {
        this->a=a;
        this->b=b;
    }
public:
    //t3=t1.TestAdd(t2);
    Test TestAdd(Test &t2)
    {
        Test tmp(this->a+t2.a,this->b+t2.b);
        return tmp;
    }
    
    //t1.TestAdd2(t2);
    //返回一个引用,相当于返回一个变量自身 
    //返回t1这个元素 this就是t1的地址
    Test& TestAdd2(Test &t2)
    {
        this->a=this->a+t2.a;
        this->b=this->b+t2.b;

        return *this;//把(&t1)又回到了t1元素
    }
public:
    void printT()
    {
        cout<<"a:"<<a<<"b:"<<b<<endl;
    }
protected:
private:
};


//全局函数的方法
//全局函数 转成 成员函数 少了一个参数
Test TestAdd(Test &t1,Test &t2)
{
    Test tmp;
    return tmp;
}


//把成员函数转成全局函数  多了一个参数
void printT(Test *pT)
{
    cout<<"a:"<<pT->a<<"b:"<<pT->b<<endl;
}


void main()
{
    Test t1(1,2);
    Test t2(3,4);
    //t1=t1+t2
    t1.TestAdd2(t2);
    t1.printT();
    system("pause");
}

void main181()
{
    Test t1(1,2);
    Test t2(3,4);
    Test t3;

    //全局函数方法
    //t1+t2
    t3=TestAdd(t1,t2);
    t3.printT();

    //成员函数方法
    {
        //先把测试案例写出来
        Test t4=t1.TestAdd(t2);//匿名对象直接转化成t4
        t4.printT();
        Test t5;
        t5=t1.TestAdd(t2);//匿名对象复制给t5
        t5.printT();
    }
    t3=t1.TestAdd(t2);

    system("pause");
}

P203  强化训练数组类--类的设计和测试程序

P204  类的实现和测试

MyArray.h

#pragma  once //保证头文件只被编译一次
#include<iostream>
using namespace std;

class Array
{
public:
	Array(int length);
	Array(const Array& obj);
	~Array();
public:
	void setData(int index,int value);
	int getData(int index);
	int length();
private:
	int m_length;
	int *m_space;
};

 

 MyArray.cpp

#include "MyArray.h"

//Array a1(10);
Array::Array(int length)
{
	if (length<0)
	{
		length=0;
		
	}
	else
	{
		m_length=length;
		m_space=new int[m_length];
	}
}

//Array a2=a1;
Array::Array(const Array& obj)
{
	this->m_length=obj.m_length;
	//深拷贝 分配内存空间
	this->m_space=new int[this->m_length];
	for(int i=0;i<obj.m_length;i++)//数组元素赋值
	{
		this->m_space[i]=obj.m_space[i];
	}
}
Array::~Array()
{
	if (m_space!=NULL)
	{
		delete[] m_space;
		m_length=0;
	}
}
//a1.setData(i,i);
void Array::setData(int index,int value)
{
	m_space[index]=value;
}
int Array::getData(int index)
{
	return m_space[index];
}
int Array::length()
{
	return m_length;
}

TestArray.cpp

#include<iostream>
#include "MyArray.h"
using namespace std;

//类的框架设计完毕
//类的测试案例
void main()
{
	Array a1(10);
	//赋值
	for(int i=0;i<a1.length();i++)
	{
		a1.setData(i,i);
	}
	//获取
	cout<<"打印a1"<<endl;
	for(int i=0;i<a1.length();i++)
	{
		cout<<a1.getData(i) <<endl;
	}

	Array a2=a1;
	cout<<"输入a2"<<endl;
	//获取
	cout<<"打印a2"<<endl;
	for(int i=0;i<a2.length();i++)
	{
		cout<<a2.getData(i)<<endl;
	}
	system("pause");
}

  


P205  友元函数

友元函数是类的好朋友,在友元函数里可以修改这个类的私有属性,言外之意友元函数破坏了类的封装性

在这个类里声明了友元函数,这个友元函数是个全局函数,要想在友元函数去修改某一个变量的某个对象的私有属性,一般友元函数至少包含原类的参数,一个赋的值

说明语句的位置与访问描述无关

#include<iostream>
using namespace std;

class A
{
public:
    //声明的位置和public private没有关系
    friend void modify(A *pA,int _a); //函数 modify是类A的好朋友
    A(int a=0,int b=0)
    {
        this->a=a;
        this->b=b;
    }
    int getA()
    {
        return this->a;
    }
private:
    int a;
    int b;
};

void modify(A *pA,int _a)
{
    //pA->a=100;
    pA->a=_a;
}




void main()
{
    A a1(1, 2);
    cout<<a1.getA()<<endl;
    modify(&a1,300);
    cout<<a1.getA()<<endl;
    system("pause");
}

this是指A类  this->指类中的属性 

类  *变量名  代表类的指针名为变量名,这个变量名就是这个类的指针

输出结果:

1

300

  


P206  友元类

  • 若B类是A类的友员类,则B类的所有成员函数都是A类的友员函数
  • 友员类通常设计为一种对数据操作或类之间传递消息的辅助类 

 

#include<iostream>
using namespace std;

class A
{
public:
    friend class B;//声明B类是A的好朋友,在B中可以访问A的私有成员 私有函数
    //声明的位置和public private没有关系
    friend void modify(A *pA,int _a); //函数 modify是类A的好朋友
    A(int a=0,int b=0)
    {
        this->a=a;
        this->b=b;
    }
    int getA()
    {
        return this->a;
    }
private:
    int a;
    int b;
};

void modify(A *pA,int _a)
{
    //pA->a=100;
    pA->a=_a;
}

//为什么设计友元类函数
// 1.java--->1.class(字节码) ==》反射机制分析1.class 找到类对象。直接修改类的私有属性
//     反射机制 成为一种标准。。。。jdk ...sun 做成标准 。。。jdk 的 api函数中有体现 
//AOP
//2 1.cpp===>汇编
// 预编译 编译  连接  生成 。。gcc -E //gcc -s  -
// gcc -o 1.exe 1.c 
// 汇编往回找。。。。很难。。。。
//3 开了一个后门 。。。friend

/*
gcc -E hello.c -o hello.i(预处理)
gcc -S hello.i -o hello.s(编译)
gcc -c hello.s -o hello.o(汇编)
gcc hello.o -o hello(链接)
以上四个步骤,可合成一个步骤

gcc hello.c -o hello(直接编译链接成可执行目标文件)
gcc -c hello.c或gcc -c hello.c -o hello.o(编译生成可重定位目标文件)
*/
class B
{
public:
    void Set(int a)
    {
        Aobj.a=a;
    }
    void PirntB()
    {
        cout<<Aobj.a<<endl;
    }
protected:
private:
    A Aobj;
};

void main()
{
    B b1;
    b1.Set(400);
    b1.PirntB();
    system("pause");
}


P207   运算符重载入门基础推演

运算符重载

  1. 运算符重载入门技术推演

  2. 运算符重载的两种方法,全局函数和成员函数

    运算符重载基础

      一元运算符重载(全局函数 类的成员函数)

      二元运算符重载

      + -

      前置++  前置--  后置++ 后置--

  3. 统一正规写法

#include<iostream>
using namespace std;

class Complex
{
public:
    int a;
    int b;
public:
    Complex(int a,int b)
    {
        this->a=a;
        this->b=b;
    }
    void printComp()
    {
        cout<<a<<"+"<<b<<"i"<<endl;
    }
private:
};

//1 定义了全局函数
Complex myAdd(Complex &c1,Complex &c2)
{
    Complex tmp(c1.a+c2.a,c1.b+c2.b);
    return tmp;
}

//2 函数名升级
Complex operator+(Complex &c1,Complex &c2)
{
    Complex tmp(c1.a+c2.a,c1.b+c2.b);
    return tmp;
}



void main()
{
    int a=0;
    int b=0;
    int c;
    c=a+b;//1. 基础数据类型  编译器已经知道如何进行运算


    //a+bi 复数运算规则
    Complex c1(1,2),c2(3,4);
    //Complex c3;//2. 类也是一种数据类型  用户自定义数据类型 C++编译器是不知道如何进行运算
    //c3=c1+c2;

    //3. c++编译器应该给程序员提供一种机制
    //让自定义数据类型有机会进行运算符操作===>运算符重载机制

    //4. 运算符重载机制
    //步骤1
    //Complex c4=myAdd(c1,c2);
    //c4.printComp();

    //步骤2  Complex c4=c1+c2
    //Complex c4=operator+(c1,c2);
    //c4.printComp();

    //步骤3  
     Complex c4=c1+c2;
     c4.printComp();
    //总结:运算符重载的本质 是 函数调用 
    //     定义 operator+ 表示重载了加法运算符 C++编译器会根据类型去找这个类型相加的函数

    system("pause");
}

1. C的提高 1-131P 时间七天 
2. C++的基础 132-286P 时间八天 
3. C++的提高 287-378P 时间五天 
4. C/C++的数据结构 379-482P 时间五天 
5. C/C++的设计模式基础 483-540P 时间三天

视频资料:https://www.bilibili.com/video/av27904891?from=search&seid=10891514449061956870



P200  const修饰的谁

#include <iostream>
using namespace std;

class Test
{
public:
    Test(int a, int b) //---> Test(Test *this, int a, int b)
    {
        this->a = a;//this就是t1取地址,谁调用它它就是谁
        this->b = b;    
    }
    void printT()
    {
        cout<<"a: " <<a <<endl;
        cout<< "b: " << this->b <<endl;
    }
    //1. const写在什么位置 没有关系
    //2. const修饰的是谁
    //   1. const修饰的是形参a  不是
    //   2. const修饰的是属性this->a this->b
    //   3. const修饰的是this指针所指向的内存空间,指针所指向的内存空间不能被修改
    void OpVar(int a,int b) const // void OpVar(const Test *const this, int a, int b) 
    {
        a=100;
        //this->a=100;
        this->b=100;
    
        cout<< "b: " << this->b <<endl;
    }
protected:
private:
    int a;
    int b;
};

void main()
{

    Test t1(1, 2);
    t1.printT();// ===> printT(&t1)
    cout<<"hello..."<<endl;
    system("pause");
    return ;
}

P202  全局函数PK成员函数

1、把全局函数转化成成员函数,通过this指针隐藏左操作数

  Test add(Test &t1, Test &t2)===》Test add( Test &t2)

2、把成员函数转换成全局函数,多了一个参数

  void printAB()===》void printAB(Test *pthis)

3、函数返回元素和返回引用

Test& add(Test &t2) //*this //函数返回引用

{

this->a = this->a + t2.getA();

this->b = this->b + t2.getB();

return *this; //*操作让this指针回到元素状态

} 

 

Test add2(Test &t2) //*this //函数返回元素

{

//t3是局部变量

Test t3(this->a+t2.getA(), this->b + t2.getB()) ;

return t3;

}

 

void add3(Test &t2) //*this //函数返回元素

{

//t3是局部变量

Test t3(this->a+t2.getA(), this->b + t2.getB()) ;

//return t3;

}
#include<iostream>
using namespace std;

class Test
{
public:
    int a;
    int b;
public:
    ~Test()
    {
        cout<<"a:"<<a<<"b:"<<b;
        cout<<"析构函数自动调用"<<endl;
    }
public:
    Test(int a=0,int b=0)
    {
        this->a=a;
        this->b=b;
    }
public:
    //t3=t1.TestAdd(t2);
    Test TestAdd(Test &t2)
    {
        Test tmp(this->a+t2.a,this->b+t2.b);
        return tmp;
    }
    
    //t1.TestAdd2(t2);
    //返回一个引用,相当于返回一个变量自身 
    //返回t1这个元素 this就是t1的地址
    Test& TestAdd2(Test &t2)
    {
        this->a=this->a+t2.a;
        this->b=this->b+t2.b;

        return *this;//把(&t1)又回到了t1元素
    }
public:
    void printT()
    {
        cout<<"a:"<<a<<"b:"<<b<<endl;
    }
protected:
private:
};


//全局函数的方法
//全局函数 转成 成员函数 少了一个参数
Test TestAdd(Test &t1,Test &t2)
{
    Test tmp;
    return tmp;
}


//把成员函数转成全局函数  多了一个参数
void printT(Test *pT)
{
    cout<<"a:"<<pT->a<<"b:"<<pT->b<<endl;
}


void main()
{
    Test t1(1,2);
    Test t2(3,4);
    //t1=t1+t2
    t1.TestAdd2(t2);
    t1.printT();
    system("pause");
}

void main181()
{
    Test t1(1,2);
    Test t2(3,4);
    Test t3;

    //全局函数方法
    //t1+t2
    t3=TestAdd(t1,t2);
    t3.printT();

    //成员函数方法
    {
        //先把测试案例写出来
        Test t4=t1.TestAdd(t2);//匿名对象直接转化成t4
        t4.printT();
        Test t5;
        t5=t1.TestAdd(t2);//匿名对象复制给t5
        t5.printT();
    }
    t3=t1.TestAdd(t2);

    system("pause");
}

P203  强化训练数组类--类的设计和测试程序

P204  类的实现和测试

MyArray.h

#pragma  once //保证头文件只被编译一次
#include<iostream>
using namespace std;

class Array
{
public:
	Array(int length);
	Array(const Array& obj);
	~Array();
public:
	void setData(int index,int value);
	int getData(int index);
	int length();
private:
	int m_length;
	int *m_space;
};

 

 MyArray.cpp

#include "MyArray.h"

//Array a1(10);
Array::Array(int length)
{
	if (length<0)
	{
		length=0;
		
	}
	else
	{
		m_length=length;
		m_space=new int[m_length];
	}
}

//Array a2=a1;
Array::Array(const Array& obj)
{
	this->m_length=obj.m_length;
	//深拷贝 分配内存空间
	this->m_space=new int[this->m_length];
	for(int i=0;i<obj.m_length;i++)//数组元素赋值
	{
		this->m_space[i]=obj.m_space[i];
	}
}
Array::~Array()
{
	if (m_space!=NULL)
	{
		delete[] m_space;
		m_length=0;
	}
}
//a1.setData(i,i);
void Array::setData(int index,int value)
{
	m_space[index]=value;
}
int Array::getData(int index)
{
	return m_space[index];
}
int Array::length()
{
	return m_length;
}

TestArray.cpp

#include<iostream>
#include "MyArray.h"
using namespace std;

//类的框架设计完毕
//类的测试案例
void main()
{
	Array a1(10);
	//赋值
	for(int i=0;i<a1.length();i++)
	{
		a1.setData(i,i);
	}
	//获取
	cout<<"打印a1"<<endl;
	for(int i=0;i<a1.length();i++)
	{
		cout<<a1.getData(i) <<endl;
	}

	Array a2=a1;
	cout<<"输入a2"<<endl;
	//获取
	cout<<"打印a2"<<endl;
	for(int i=0;i<a2.length();i++)
	{
		cout<<a2.getData(i)<<endl;
	}
	system("pause");
}

  


P205  友元函数

友元函数是类的好朋友,在友元函数里可以修改这个类的私有属性,言外之意友元函数破坏了类的封装性

在这个类里声明了友元函数,这个友元函数是个全局函数,要想在友元函数去修改某一个变量的某个对象的私有属性,一般友元函数至少包含原类的参数,一个赋的值

说明语句的位置与访问描述无关

#include<iostream>
using namespace std;

class A
{
public:
    //声明的位置和public private没有关系
    friend void modify(A *pA,int _a); //函数 modify是类A的好朋友
    A(int a=0,int b=0)
    {
        this->a=a;
        this->b=b;
    }
    int getA()
    {
        return this->a;
    }
private:
    int a;
    int b;
};

void modify(A *pA,int _a)
{
    //pA->a=100;
    pA->a=_a;
}




void main()
{
    A a1(1, 2);
    cout<<a1.getA()<<endl;
    modify(&a1,300);
    cout<<a1.getA()<<endl;
    system("pause");
}

this是指A类  this->指类中的属性 

类  *变量名  代表类的指针名为变量名,这个变量名就是这个类的指针

输出结果:

1

300

  


P206  友元类

  • 若B类是A类的友员类,则B类的所有成员函数都是A类的友员函数
  • 友员类通常设计为一种对数据操作或类之间传递消息的辅助类 

 

#include<iostream>
using namespace std;

class A
{
public:
    friend class B;//声明B类是A的好朋友,在B中可以访问A的私有成员 私有函数
    //声明的位置和public private没有关系
    friend void modify(A *pA,int _a); //函数 modify是类A的好朋友
    A(int a=0,int b=0)
    {
        this->a=a;
        this->b=b;
    }
    int getA()
    {
        return this->a;
    }
private:
    int a;
    int b;
};

void modify(A *pA,int _a)
{
    //pA->a=100;
    pA->a=_a;
}

//为什么设计友元类函数
// 1.java--->1.class(字节码) ==》反射机制分析1.class 找到类对象。直接修改类的私有属性
//     反射机制 成为一种标准。。。。jdk ...sun 做成标准 。。。jdk 的 api函数中有体现 
//AOP
//2 1.cpp===>汇编
// 预编译 编译  连接  生成 。。gcc -E //gcc -s  -
// gcc -o 1.exe 1.c 
// 汇编往回找。。。。很难。。。。
//3 开了一个后门 。。。friend

/*
gcc -E hello.c -o hello.i(预处理)
gcc -S hello.i -o hello.s(编译)
gcc -c hello.s -o hello.o(汇编)
gcc hello.o -o hello(链接)
以上四个步骤,可合成一个步骤

gcc hello.c -o hello(直接编译链接成可执行目标文件)
gcc -c hello.c或gcc -c hello.c -o hello.o(编译生成可重定位目标文件)
*/
class B
{
public:
    void Set(int a)
    {
        Aobj.a=a;
    }
    void PirntB()
    {
        cout<<Aobj.a<<endl;
    }
protected:
private:
    A Aobj;
};

void main()
{
    B b1;
    b1.Set(400);
    b1.PirntB();
    system("pause");
}


P207   运算符重载入门基础推演

运算符重载

  1. 运算符重载入门技术推演

  2. 运算符重载的两种方法,全局函数和成员函数

    运算符重载基础

      一元运算符重载(全局函数 类的成员函数)

      二元运算符重载

      + -

      前置++  前置--  后置++ 后置--

  3. 统一正规写法

#include<iostream>
using namespace std;

class Complex
{
public:
    int a;
    int b;
public:
    Complex(int a,int b)
    {
        this->a=a;
        this->b=b;
    }
    void printComp()
    {
        cout<<a<<"+"<<b<<"i"<<endl;
    }
private:
};

//1 定义了全局函数
Complex myAdd(Complex &c1,Complex &c2)
{
    Complex tmp(c1.a+c2.a,c1.b+c2.b);
    return tmp;
}

//2 函数名升级
Complex operator+(Complex &c1,Complex &c2)
{
    Complex tmp(c1.a+c2.a,c1.b+c2.b);
    return tmp;
}



void main()
{
    int a=0;
    int b=0;
    int c;
    c=a+b;//1. 基础数据类型  编译器已经知道如何进行运算


    //a+bi 复数运算规则
    Complex c1(1,2),c2(3,4);
    //Complex c3;//2. 类也是一种数据类型  用户自定义数据类型 C++编译器是不知道如何进行运算
    //c3=c1+c2;

    //3. c++编译器应该给程序员提供一种机制
    //让自定义数据类型有机会进行运算符操作===>运算符重载机制

    //4. 运算符重载机制
    //步骤1
    //Complex c4=myAdd(c1,c2);
    //c4.printComp();

    //步骤2  Complex c4=c1+c2
    //Complex c4=operator+(c1,c2);
    //c4.printComp();

    //步骤3  
     Complex c4=c1+c2;
     c4.printComp();
    //总结:运算符重载的本质 是 函数调用 
    //     定义 operator+ 表示重载了加法运算符 C++编译器会根据类型去找这个类型相加的函数

    system("pause");
}

猜你喜欢

转载自www.cnblogs.com/yangyuqing/p/10419904.html