1,动态数组
C++中动态数组创建使用new,释放使用delet [] 数组名;
如下例,动态统计学生姓名、年龄和性别:
Student.h
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <string>
class Student{
private:
std::string name;
int age;
bool sex;
public:
void Init(std::string n,int ag,bool s);
void Print();
};
#endif
Student.cpp
#include <iostream>
#include <string>
// strlen,strcpy,strcmp -> cstring
#include "Student.h"
using namespace std;
void Student::Init(string name,int age,bool sex){
this->name = name;
this->age = age;
this->sex = sex;
}
void Student::Print(){
cout << "Name:" << name << endl;
cout << "Age:" << age << endl;
cout << "Sex:" << sex << endl;
}
StudentTest.cpp
#include <string>
#include <iostream>
#include "Student.h"
using namespace std;
void Handle(int count){
string name;
int age;
bool sex;
Student* ps = new Student[count];
for(int i=0;i != count;i++){
cin >> name >> age >> sex;
ps[i].Init(name,age,sex);
}
for(int i=0;i != count;i++){
ps[i].Print();
}
delete [] ps;
}
class Empty{};
int main(){
cout << "Please Input Num of Students:";
int count;
cin >> count;
Handle(count);
cout << sizeof(Empty) << endl;
}
其中:
1,空结构体与空类的大小(sizeof
)为1
,主要在于初始化/实例化时,编译器给变量/对象分配内存(地址),内存最小单位为1个字节。
2,this
指针
- 作用域 – 类内部
- 特点
- 类的一个自动生成、自动隐藏的私有成员
- 每个对象仅有一个
this
指针 - 当一个对象被创建时,
this
指针就存放指向对象数据的首地址
2,构造函数与两种访问方式
- 构造函数
- 不能有返回值类型
- 名字与类名保持一致
- 在对象被创建时自动执行
- 无参数的构造函数也叫
默认构造函数
- 类里不写构造函数,实例化时,自动添加默认构造函数
- 当手动写有参数的构造函数时,如果想构造类的数组,一定得手动添加默认构造函数
- 构造函数根据参数个数,自动匹配对应的构造函数,个数一样的时候匹配类型
student.h
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <string>
class Student{
private:
std::string name;
int age;
bool sex;
public:
void Init(std::string n,int ag,bool s);
Student(std::string name,int age,bool sex);
Student(std::string name,int age);
Student(std::string name,bool sex);
void Print();
};
#endif
Student.cpp
#include <iostream>
#include <string>
// strlen,strcpy,strcmp -> cstring
#include "Student.h"
using namespace std;
void Student::Init(string name,int age,bool sex){
this->name = name;
this->age = age;
this->sex = sex;
}
Student::Student(std::string name,int age,bool sex){
cout << "Student A" << endl;
this->name = name;
this->age = age;
this->sex = sex;
}
Student::Student(std::string name,int age){
cout << "Student B" << endl;
this->name = name;
this->age = age;
sex = 1;
}
Student::Student(std::string name,bool sex){
cout << "Student C" << endl;
this->name = name;
this->age = 7;
this->sex = sex;
}
void Student::Print(){
cout << "Name:" << name << endl;
cout << "Age:" << age << endl;
cout << "Sex:" << sex << endl;
}
StudentTest.cpp
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
int main(){
// 局部变量
Student student1("zhangs",13,0);
student1.Print();
Student student2("ls",14);
student2.Print();
Student student3("wangwu",true);
student3.Print();
// 动态内存
Student* pstudent1 = new Student("zhangs",13,0);
pstudent1->Print();
Student* pstudent2 = new Student("ls",14);
pstudent2->Print();
Student* pstudent3 = new Student("wangwu",true);
pstudent3->Print();
delete pstudent1;
delete pstudent2;
delete pstudent3;
// 数组
Student stduent;
stduent.Print();
Student students[3];
return 0;
}