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/C++基础——set的基本操作总结
- 【基础备忘】C++单链表基本操作
- C/C++基础:set的基本操作总结及注意细节问题
- C/C++基础——vector的基本操作总结
- C++基础的不能再基础的学习笔记——顺序容器(基本操作)
- C/C++基础——vector的基本操作总结
- C/C++基础——set的基本操作总结
- c++基础值链表基本操作
- C/C++基础——map的基本操作总结
- C/C++基础——map的基本操作总结
- C++基础知识(四)文件的基本操作
- C++ map的基本操作和使用
- c++中的字符串基本操作
- C/C++基础知识:函数指针和指针函数的基本概念
- 【编程语言】C++--map的基本操作和使用
- C++ map的基本操作和用法
- C++控制台操作(基本操作的代码)
- C++ 二叉树的基本操作
- 一步一步教你使用AgileEAS.NET基础类库进行应用开发-基础篇-演示ORM的基本操作
- 【原创】StreamInsight查询系列(七)——基本查询操作之基础排序