/*test.cpp*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
private:
int x, y;
char *name;
public:
Student(int a, int b, char *str);
~Student();
void display(void);
};
Student::Student(int a, int b, char *str)
{
x=a;
y=b;
name = new char[20];
strncpy(name, str, 20);
cout << "Init succeed." << endl;
}
Student::~Student()
{
delete []name;
cout << "Destroy completed." << endl;
}
void Student::display(void)
{
printf("x: %d, y: %d\n", x, y);
printf("name: %s\n", name);
return ;
}
class Object
{
public:
int x;
int y;
};
int main()
{
char buf[20] = "lifeiheng";
Student *stu = new Student(1, 2, buf);
stu->display();
cout<<"size: "<<sizeof(Student)<<endl;
Object obj = {2, 3};
cout << obj.x << "--"<< obj.y << endl;
delete stu;
return 0;
}
c++超基础:类的基本操作
最新推荐文章于 2023-02-27 15:08:15 发布