您的位置:首页 > 编程语言 > C语言/C++

c++超基础:类的基本操作

2014-12-26 23:45 253 查看
/*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++ 超基础