一、封装
封装:是对信息的隐藏及模块化,是一种使接口和实现相分离的方法。在计算机语言的学习中我们有过数据的封装,也进行过函数封装。而对象的封装则是将数据及其操作封装成一个整体对象,使程序对数据的访问或修改只能通过对象对外提供的接口才能进行。
例如:以下程序通过模块化编程,将一个程序封装成三个文件,分别为:Person.h、Person.cpp、main.cpp
其中:Person.h 声明类;Person.cpp类方法的实现;main.cpp程序主要功能实现。具体程序代码如下:
/*******************Person.h***********************/
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name = "lili";
public:
Person();
void showName();
void setName(string str);
};
#endif // PERSON_H
/**************Person.cpp**********************/
#include "person.h"
Person::Person()
{
cout<<"Calling the constructor"<<endl;
}
void Person::showName(){
cout<<"my name is "<<name<<endl;
}
void Person::setName(string str){
name = str;
}
/****************main.cpp*********************/
#include "person.h"
int main()
{
cout<<"kakaka"<<endl;
Person *p1 = new Person();
p1->showName();
p1->setName("ivan");
p1->showName();
return 0;
}
二、重载
在c++语言中的重难点,无非是重载、多态、模板。在这里我们先介绍重载。
重载(静态绑定:编译期绑定):同名不同参(函数名相同,参数类型、个数、顺序不同),(调用时)智能匹配。
原理:编译器通过对比你使用的参数类型与定义的参数类型,决定最适合的定义。
1、函数重载:
功能类似的函数,仅细节不同,可取相同的名字,自动适配,避免麻烦。(注意:不可以通过返回值类型区分)
#if 1
/**************构造函数重载****************/
#include <iostream>
#include<string>
using namespace std;
class Person
{
private:
string name = "lili";
public:
//构造函数(多个同名-->重载)
Person();
Person(string str);
void showName();
};
Person::Person(){
cout<<name+"create object 1"<<endl;
}
Person::Person(string str)
{
name = str;
cout<<name+"create object 2"<<endl;
}
void Person::showName(){
cout<<"my name is "<<name<<endl;
}
int main()
{
Person *p1 = new Person();
p1->showName();
Person *p2 = new Person("dahua");
p2->showName();
return 0;
}
#endif
2、同名方法重载
//例: 同名方法重载(注意:不能通过返回类型来重载)
{//person.h
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class Person
{
private:
string name="baby";
int age=20;
public:
Person();
void show();
void show(int i);
void show(string str);
};
#endif // PERSON_H
}
{//person.cpp
#include "person.h"
Person::Person(){
}
void Person::show(){
cout<<"name:"+ name + " age:"<<age<<endl;
}
void Person::show(int i){
cout<<"age:"<<age<<endl;
}
void Person::show(string str){
cout<<"name:"+ name<<endl;
}
}
{//main.cpp
#include "person.h"
int main(int argc, char *argv[])
{
Person *p1 = new Person();
//同名方法,通过不同参数差异来重载(注意:不能通过返回类型来重载)
p1->show();
p1->show(1);
p1->show("str");
return 0;
}
}
3、运算符重载:即为在类中为已有运算符赋予新含义的过程。
为什么需要运算符重载: 因为运算符操作仅限于基本数据类型,在有些时候我们需要对自定义数据类型进行操作,这时要想使用运算符,则需要进行运算符重载。
运算符重载函数相对于类有两种存在方式:(1)重载为类的成员函数;(2)重载为类的友元函数。下面讲的都是重载为成员函数的内容:
基本格式:
类型说明符 operator 运算符 (参数列表)
{
函数体; //实现运算符功能的代码
}
//例子:
bool operator >= (const Student & stu) const
{
}
运算符重载基本规则 |
---|
1、只能重载已有的运算符,并且以下5个运算符不能被重载:成员访问运算符 “.”,成员指针运算符 " * ",域运算 “::”,条件运算符“?:”,sizeof运算符; |
2、运算符重载后不改变优先级、结合性、操作数个数; |
3、运算符重载后不改变原有语义; |
4、重载后的操作对象至少有一个自定义类对象(或类对象引用),即不能为基本数据类型重载运算符; |
/********************mhead.h*************************/
#ifndef MHEAD_H
#define MHEAD_H
//运算符重载
//双目运算符重载:重载加法运算符求体积
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(const string& ="",double=0);
bool operator >= (const Student&) const;
string GetName()const;
double GetScore()const;
private:
string strName;
double dScore;
};
#endif // MHEAD_H
/***********************main.cpp***************************/
#include <QCoreApplication>
#include "mhead.h"
Student::Student(const string& name,double score)
{
strName = name;
dScore = score;
}
//常成员函数:用来比较两个对象的数据成员dScore的大小
bool Student:: operator >=(const Student& stu) const
{
return this->dScore >= stu.dScore;
}
string Student::GetName() const
{
return strName;
}
double Student::GetScore() const
{
return dScore;
}
int main(int argc, char *argv[])
{
Student s1("jan",89),s2("ken",98);
cout<<"成绩较好的学生姓名、成绩:";
if(s1>=s2)
{
cout << s1.GetName()<<" "<<s1.GetScore()<<endl;
}else{
cout << s2.GetName()<<" "<<s2.GetScore()<<endl;
}
return 0;
}