zoukankan      html  css  js  c++  java
  • 构造函数与析构函数2

    // 构造函数与析构函数2.cpp : 定义控制台应用程序的入口点。
    //学习动态内存单元的申请

    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    class Student
    {
    public:
        Student();
        Student(int pid, char*pname, float s);
        void modify(float s);
        void display();
        ~Student();
    private:
        int id;
        char *name;
        float score;
    };


    Student::Student()
    {
        id = 0;
        name = new char[11];
        strcpy(name,"no name");
        score = 0;
    }

    Student::Student(int pid, char * pname, float s)
    {
        id = pid;
        name = new char[strlen(pname) + 1];
        strcpy(name, pname);
        score = s;
    }

    void Student::modify(float s)
    {
        score = s;
    }

    void Student::display()
    {
        cout << "id" << id << endl;
        cout << "name" << name << endl;
        cout << "score" << score << endl;
    }

    Student::~Student()
    {
        delete[] name;
    }

    int main()
    {
        Student s1;
        s1.display();
        Student s2(1511435, "Alen Turing", 95);
        s2.display();
        s2.modify(90);
        s2.display();
        system("pause");
        return 0;
    }

  • 相关阅读:
    tctip demo页面>
    tctip demo页面>
    tctip demo页面>
    tctip demo页面>
    tctip demo页面>
    tctip demo页面>
    tctip demo页面>
    tctip demo页面>
    tctip demo页面>
    sql 随笔
  • 原文地址:https://www.cnblogs.com/summercloud/p/5522124.html
Copyright © 2011-2022 走看看