函数的重载,重定义,重写

函数的重载,重定义,重写

  1. 重载的定义

函数重载就是在同一个类里面,通过改变参数的类型,参数的个数,不同类型参数的顺序。但是不能通过返回值的类型来重载函数。换句话说,就是用来实现功能类似,但是处理的参数类型不同的函数。

  1. 重载的实现

类的无参的构造函数,和有参的够着函数。

#ifndef _STUDENT_H
#define _STUDENT_H
#include <string>

using namespace std;

class Student
{
public:
    1 Student();
    2 //Student(int age = 10, int height = 170, string name = "");//这个就是进函数的重载,大家注意后面面函数的调用,1,2行,不能同时,因为同时出现就会导致出错信息!(student类包含多个默认的构造函数,我觉得应该是调用不明确。)引发的错误 
    Student(int age, int height,string name);

    //重载overloading
    //int sum();
    int sum(int initialValue = 10);
    int sum(int , int );
    void sum(int , string );
    int sum(string , int );

    ~Student();
    int number;

    void setAge(int age);
    int getAge();//获取
    void setHeight(int height);
    int getHeight();
    void setName(string name);
    string getName();

private:
    int age;
    int height;
    std::string name;
};

//void system(const char *);

#endif

“`

//student.cpp(用来定义吧)

#include "Student.h"
#include<iostream>
using namespace std;
int a = 88;
void tempForTest();
void tempForTest()
{

}


 Student::Student()
 {
    age = 24;
    height = 165;
    name = "temp";
 }

Student::Student(int age, int height, string name)
{
    this->age = age;
    this->height = height;
    this->name = name;
}

Student::~Student()
{
}

void Utility()
{

}

void Student::setAge(int age)
{
    this->age = age;
}

int Student::getAge()
{
    return this->age;
}

void Student::setHeight(int height)
{

}

int Student::getHeight()
{
    return this->height;
}

void Student::setName(string name)
{

}

string Student::getName()
{
    return this->name;
}

int Student::sum(int initialValue)
{
    if (initialValue > 0)
    {
        for (int i = 0; i < 10; i++)
        {
            initialValue++;
        }
    }
    return initialValue;

}
//int Student::sum(int initialValue = 10)
//{
//  return 11111;
//  cout << "这个是sum(int initialValue = 10 )" << 1111<< endl;
//}

int Student::sum(int i, int j )
{

    cout << "这个是sum(int i,int j )" << i+j << endl;
    return i + j;
}
 void Student::sum(int i, string s)
{
     cout << "这个是sum(int i, string s)" << i << endl;
}
int Student::sum(string s, int i)
{

    cout << s;
    cout << "这个是sum(string s, int i)" << i << endl;
    return i;
}

下面是main() 函数

#include <iostream>
#include "Student.h"
#include "ABanana.h"
using namespace std;
extern int a;
void tempForTest();

//using namespace std;

namespace WangPuyao
{
    void system(const char *)
    {
        int i = 10;
        //std::system("pause");
    }
}

void ReferenceTest()
{
    int a = 0;



}

void main()
{
    ReferenceTest();


    tempForTest();
    int temp = 10;
    a = 10;
    Student student;
 /******通过改变参数,来调用不同的构造函数,这样就可以区分调用了哪一个构造函数。****/
    int a=student.getAge();
    student.sum(1, 2);
    student.sum(1, "tracy");
    student.sum(1);
    student.sum("lucy", 2);


    std::system("pause");
    std::cout << "hello world!!!" << std::endl;

    WangPuyao::system("");
    std::cout << "hello world, again!" << std::endl;


    int sumValue = student.sum(15);
    sumValue = student.sum();

    student.setAge(10);
    //student.age = 10;
    student.number = 10;

    Student Tracy;
    Student Tracy(22, 160, "HanLili");

    //HanLili.sum();

    int Tracy_age = Tracy.getAge();
    Student Chen;


    //student.age = 10;

    std::cout << "hello, world" << std::endl;
    system("pause");

}

总之一句话,就是含有参数个数,类型,顺序相同的构造函数,不能同时拥有给定默认的参数的函数和不给定参数的函数。以为这样没有构造出重载。

函数的重写

  1. 重写的定义
    必须发生在父类与子类之间。(同时分为两类)参数类型,返回的值类型,参数的个数必须是一致的。 当我们对别人提供好的类的方法感觉不是太满意时,我们就可以通过继承这个类然后重写其方法改成我们需要的逻辑。
    函数的重定义

重定义的概念

子类重新定义父类中有相同名称的非虚函数,参数列表可以相同可以不同,会覆盖其父类的方法,未体现多态。

这三种方法的对比
这里写图片描述
这里写图片描述
**这个没有写完,等我实现了函数的重写和重定义可以把代码再放进来

猜你喜欢

转载自blog.csdn.net/yychentracy/article/details/81412060
今日推荐