C++基础之2类与对象

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值