我们在学习C++的过程中类的设计是避免不了的,那么接下来我们就类的构造函数进行解说,因为博主也是才接触C++,所以都是从新手方面来考虑,仅供大家参考。如果你是C++大神,那么很抱歉,我的这些简介对你来说也就不值一提了。
1. 什么是构造函数?
2. 怎么写构造函数?
3.什么是缺省构造函数。
4. 构造函数的简单应用。
下面我们就这具体来解决这几个问题————
1. 什么是构造函数?
用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中。该函数无返回类型!(注意:是“无”! 不是空!(void))。但是该函数有返回值(这个对象),特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类型的不同来区分它们 即构造函数的重载。(函数的重载会在下一篇博客中进行详细的讲解)并且一个对象也只能在被创建的时候调用构造函数,即一个对象只能调用一次构造函数(构造函数就相当于一个人的出生,析构函数就是一个人的终结。)
2. 怎么写构造函数?
怎么写构造函数我们就从代码来演示吧:
class Fruits {
private:
char *Name;
int Price;
char *Breed;
int Amount;
public:
Fruits() {
cout << "constructor call" << endl;
}
Fruits(int price) {
Price = price;
cout << "constructor call" << endl;
}
Fruits(char *name,int price) {
Price = price;
Name = name;
}
Fruits(int price,char *breed,int amount) {
Breed = breed;
Amount = amount;
Price = price;
cout << "constructor call" << endl;
}
Fruits(char *name, int price, char *breed, int amount)
{
Breed = breed;
Amount = amount;
Price = price;
Amount = amount;
cout << "constructor call" << endl;
}
};
在这个类里的所有函数都是class Fruits的构造函数,在C++中是以函数原型来区分函数的,函数原型: 函数返回值类型 + 函数名 + 形参(形参的类型+ 形参的个数)
类中的函数也可以在类中声明 在类的外部定义
class Fruits {
private:
char *Name;
int Price;
char *Breed;
int Amount;
public:
Fruits();
Fruits(int);
Fruits(char *, int );
Fruits(int , char *, int);
Fruits(char *, int , char *, int );
};
Fruits::Fruits() {
cout << "constructor call" << endl;
}
Fruits::Fruits(int price) {
Price = price;
cout << "constructor call" << endl;
}
Fruits::Fruits(char *name, int price) {
Price = price;
Name = name;
}
Fruits::Fruits(int price, char *breed, int amount) {
Breed = breed;
Amount = amount;
Price = price;
cout << "constructor call" << endl;
}
Fruits::Fruits(char *name, int price, char *breed, int amount)
{
Breed = breed;
Amount = amount;
Price = price;
Amount = amount;
cout << "constructor call" << endl;
}
细心的读者一定会发现在类中构造函数的声明中只有形参的类型,那么会不会有错呢?不会。(C++函数重载)当然加上形参变量也没错就和上边的一样。因为C++在编译的时候 第一步 访问数据成员 ;第二步 扫描函数的声明但不识别函数体 ;第三步 识别函数体并改写函数(这里涉及到指针this,关于this会在另一篇博文中进行详解)。
:: 作用域解析符
Fruits::Fruits(char *name, int price, char *breed, int amount)
Fruits::就是说Fruits这个函数是Fruits类内部的函数。
同时肯定也会有人问函数定义写在类的外部和写在类的内部有什么区别?是的,这两种写法是有区别的。(涉及到内联函数inline会在下一篇博客中进行详细的讲解)
调用方式不同,在类里面定义的是内联函数
//调用 的时候不发生控制权转移,作为函数体本身一个模块进行调用
在类外面定义的函数
//情况相反,调用 的时候需要开辟一部分空间
//总结就是类里面定义的调用更快,更节省内存
3. 什么是缺省构造函数
在C++的一个类中,如果构造函数没有参数,或者构造函数的所有参数都有默认值,就可以称其为缺省构造函数。一个类中,只能有一个缺省构造函数。当你在设计一个类的时候没有写出构造函数,编译器会在编译的时候给出缺省构造函数。
#include <stdlib.h>
#include <iostream>
using namespace std;
class Fruits {
private:
char *Name;
int Price;
char *Breed;
int Amount;
};
int main()
{
Fruits fone; /*因为我们在设计类的时候并没有写构造函数,所以编译器在编译的时候给出缺省构造函数对这个对象进行初始化,而且不能写成Fruits fone();*/
getchar();
return 0;
}
4. 构造函数的简单应用。
(因为C++是基于对象的编程,所以我认为构造函数就好比是在你出生时你父母所能给与你的身份一样。而你在就如同C++中的对象,你的身份就是类的设计中的属性值(数据成员)得组合)
#include <stdlib.h>
#include <iostream>
using namespace std;
class Fruits {
private:
char *Name;
int Price;
char *Breed;
int Amount;
public:
Fruits() {
cout << "constructor call" << endl;
}
Fruits(int price) {
Price = price;
cout << "constructor call" << endl;
}
Fruits(char *name, int price) {
Price = price;
Name = name;
}
Fruits(int price, char *breed, int amount) {
Breed = breed;
Amount = amount;
Price = price;
cout << "constructor call" << endl;
}
Fruits(char *name, int price, char *breed, int amount)
{
Breed = breed;
Name = name;
Price = price;
Amount = amount;
cout << "constructor call" << endl;
}
void View() {
cout << "Name: " << Name << endl;
cout << "Price: " << Price << endl;
cout << "Breed:" << Breed << endl;
cout << "Amount: " << Amount << endl;
}
};
int main()
{
Fruits fone;
fone.View();
Fruits ftwo(10);
ftwo.View();
Fruits fthr("apple", 10);
fthr.View();
Fruits ffou(10, "new", 100);
ffou.View();
Fruits ffiv("apple", 10, "new", 100);
ffiv.View();
getchar();
return 0;
}
构造函数的参数的不同意味着**并不是所有的数据成员**(设计的类中的变量(类的属性)
char *Name; int Price; char *Breed;int Amount;)都需要被初始化,