构造函数与析构函数2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了构造函数与析构函数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;
}
以上是关于构造函数与析构函数2的主要内容,如果未能解决你的问题,请参考以下文章