数据结构复习笔记:C++程序设计(二)

来源:互联网 发布:sql引擎 编辑:程序博客网 时间:2024/06/05 22:40

1、动态存储分配:
(1)new操作符:进行动态存储分配,返回一个指向所分配空间的指针。

#include <iostream>using namespace std;int main(int argc, char* argv[]) {     int *p1 = new int(8);     cout << p1 << endl; // 输出指针p的地址     cout << *p1 << endl; // 8     delete p1;     int *p2;     p2 = new int(2017);     delete p2;     return 0;}

(2)一维数组动态存储分配:(对比静态数组和动态数组)

#include <iostream>using namespace std;int main(int argc, char* argv[]) {    float f1[2] = {5, 8}; // 静态数组,在栈内存,性能高,但是大小限定    cout << *f1 << endl; // 5    cout << f1[0] << endl; // 5    cout << f1[2] << endl; // 越界, 虽然可能正常运行,但是数据无效,并且可能导致冲突崩溃    float *f2 = new float[2]; // 动态数组,在堆内存,可以不限定数组大小,2只是初始化的大小空间    f2[0] = 8, f2[1] = 10;    f2[2] = 20;    cout << *f2 << endl; // 8    cout << f2[0] << endl; // 8    cout << f2[2] << endl; // 20,不越界正常,但是不是存在预先指定的内存地址中,存在无效的可能,不要如此用,可以扩大内存将原有数据拷贝过去    delete[] f2;    return 0;}

(3)二维数组动态存储分配:

#include <iostream>using namespace std;int main(int argc, char* argv[]) {    int num1[2][3] = {{0, 1, 3} , {2, 5, 8}}; // 静态二维数组    cout << num1[1][1] << endl; // 5    char c2[][3] = {'a', 'b', 'c', 'd', 'e', 'f'}; // 静态二维数组(不设置一维数目,根据二位数量和赋值数量自动计算出6/3=2)    cout << c2[1][1] << endl; // e    int **num2 = new int*[3]; // 动态二维数组,生产环境中这里的3通常是动态传入,表示指针数组num2包含3个元素    num2[2] = new int[5];    num2[2][4] = 8;    cout << num2[2][4] << endl; // 8    delete[] num2[2];    delete[] num2;    return 0;}

2、类的使用(继承、多态、虚函数、友元函数):

#include <iostream>#include <string>using namespace std;class Person {    protected:        string name; // 或 char *name;        unsigned int age;    public:        Person() {} // 默认构造函数        Person(char *name, unsigned int age);// 构造函数,将实现放到外部        ~Person() {} // 析构函数        virtual void saySth() const;        virtual void work() const = 0; //  = 0标记将其定义为纯虚函数,抽象方法,父类不能实现,由派生类实现}; // 注意c++这里要加上分号Person::Person(char *name, unsigned int age) {    this->name = name;    this->age = age;}void Person::saySth() const {    cout << "Hello, person!" << endl;}class Student : public Person { // 继承的权限关键词public必需加上,否则默认是private,无法利用类型转换,实现多态    private:        float score;    public:        Student();         Student(char *name, unsigned int age, float score);        ~Student() {}        int operator-(const Student& stu) const; // 重载“-”运算符计算两个学生的成绩差,const关键词不允许修改类的成员        friend float addOne(float num); // 友元函数,类外定义,不为当前类独有,但是可以访问类的私有成员        virtual void saySth() const;        virtual void work() const;        static void sayIdentity(); // 静态方法        void sayHi() const { // 实例方法, 将实现放到内部            cout << name << "->" << age << ", " << score << ": Hi!" << endl;        }        void setScore(float score) {            this->score = score;        }};Student::Student() {    this->name = "Willson";    this->age = 18;    this->score = 100;}Student::Student(char* name, unsigned int age, float score) : Person(name, age) { // Person后面的name和age是实参,由Student构造方法的形参接收的实参传入,Person前面的冒号表示调用父类的某个构造函数    this->score = score;}void Student::sayIdentity() { // 实现部分不需要写static    cout << "student" << endl;}void Student::saySth() const {    cout << "Hello, student!" << endl;}void Student::work() const {    cout << "working" << endl;}int Student::operator-(const Student& stu) const {    return this->score - stu.score;}float addOne(float num) { // 友元函数,不要写成Student::addOne    return num + 1;}int main(int argc, char* argv[]) {    Student *stu1 = new Student("Tafeng", 10, 99.5); // new方式定义的,需要delete释放内存    stu1->sayHi(); // Tafeng->10, 98.5: Hi!    delete stu1;    Person *p = new Student(); // 虚函数,多态    p->saySth(); // Hello, student!    delete p;    Student stu2; // 直接定义的,不需要手动释放内存    stu2.sayHi(); // Willson->18, 100: Hi!    Student stu3;    stu3.setScore(75);    int scoreDiff = stu2 - stu3; // 不能是stu1 - stu2,因为重载的参数Student& stu不是指针    cout << scoreDiff << endl; // 100 - 75 = 25    int newScore = addOne(75);    cout << newScore << endl; // 76    Student::sayIdentity(); // student    return 0;}

3、设计测试数据:
(1)黑盒法:考虑程序的实际功能,而不是实际的代码。常用方法:I/O分类法,因果图。
(2)白盒法:通过检查程序代码来设计测试数据,使测试数据的执行结果能够很好的覆盖程序的语句及执行路径。要求每一个测试集对程序中的每一条语句至少执行一次(语句覆盖)。

原创粉丝点击