云栖问答

找不到答案?去提问题

c++ 2016-03-06 11:55:29

c++分配内存空间

#include
#include
using namespace std;
class Person
{
private:
char *name;
public:
Person(char *n1):name(n1){strcpy(name,n1);}
void PrintName() {cout<<"The name is:";}
};
class Student:public Person
{
private:
long int Number;
public:
Student(char *n2,long int n3):Person(n2) {Number=n3;}
void PrintInfo()
{
cout<<"The number is:";
}
};
int main()
{
Person a("张三");
cout<<a.PrintName<<endl;
Student b("张三",142180217);
cout<<b.PrintInfo<<endl;
return 0;
}

1个回答

0

a123456678

 #include<iostream>
#include<string>
using namespace std;
class Person
{
private:
    char *name;
public:
    Person(char *n1):name(n1){strcpy(name,n1);}
    void PrintName() {cout<<"The name is:" << name; }
};
class Student:public Person
{
private:
    long int Number;
public:
    Student(char *n2,long int n3):Person(n2) {Number=n3;}
    void PrintInfo()
    {
        cout<<"The number is:" << Number;
    }
};
int main()
{
        char ch[] = "张三";
    Person a(ch);
    a.PrintName();
        cout << endl;
    Student b(ch,142180217);
    b.PrintInfo();
        cout << endl;
    return 0;
}

1
GO