码迷,mamicode.com
首页 > 其他好文 > 详细

构造函数与析构函数2

时间:2016-05-24 06:56:47      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

// 构造函数与析构函数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;
}

构造函数与析构函数2

标签:

原文地址:http://www.cnblogs.com/summercloud/p/5522124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!