经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++对象与继承使用中一些问题介绍
来源:jb51  时间:2022/1/2 12:22:23  对本文有异议

定义抽象类

  1. class Person {
  2. public:
  3. virtual void born() = 0;
  4. };

类的继承

  1. class Man :public Person {
  2. public:
  3. void born() {
  4. cout << " 出生了一个男人" << endl;
  5. }
  6. };

使用new关键字实例化对象,只能用指针变量来接收

因此,在对象的实例化,作为函数的参数和返回值时,都用要使用指针

  1. Person* generatePersion(Person* person1) {
  2. Person* person = new Man();
  3. retrun person;
  4. }

使用普通的对象属性及方法时使用.来引用,但是用指针表示的对象则使用->

  1. Student stu1("张三",18,"北京"); // 直接用变量实例化对象
  2. Student *stu2 = new Student("李四",20,"上海"); // 通过指针实例化对象
  3. stu1.study();
  4. stu2->study();

定义类的时候,属性需要赋初始值的请况

  1. class StudentId {
  2. private:
  3. static StudentId* si; // 必须用static修饰
  4. static string id; // 必须用static修饰
  5. };
  6. string StudentId::id = "20200001";
  7. StudentId* StudentId::si = NULL;

类的前置声明

c++在使用这个类之前,必须要定义这个类,不然编译器不知道有这个类
因此,当两个类互相嵌套时,可能会报错,解决的方法就是使用前置声明
如果在类的方法实现过程中,还用到了另一个类的相关方法
那么最好是将类方法的定义和实现分开写

  1. class AbstractChatroom; // 类的前置声明
  2.  
  3. class Member{
  4. protected:
  5. AbstractChatroom *chatroom; // 使用之前必须先定义
  6. void setChatroom(AbstractChatroom *chatroom) { // 类方法可以在类定义的时候就实现
  7. this->chatroom = chatroom;
  8. }
  9. void chatroom_play(); // 当方法内部需要使用到前置声明类中的方法时,只能先定义,后实现
  10. };
  11.  
  12. class AbstractChatroom{ // 类的具体定义
  13. public:
  14. Member member; // 类的相互嵌套
  15. virtual void sendText(string from,string to,string message) = 0;
  16. void play(){
  17. cout << "在聊天室里玩耍!" << enld;
  18. }
  19. };
  20.  
  21. void Member::chatroom_play(){ // 类方法的具体实现
  22. chatroom -> play();
  23. }

命名空间的使用

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. namespace my_namespace{ // 定义命名空间
  5.  
  6. class Student{ // 命名空间中的对象
  7. public:
  8. string name;
  9. int age;
  10. string home;
  11. Student(string name, int age, string home);
  12. string getName();
  13. int getAge();
  14. string getHome();
  15. void setName(string name);
  16. void setAge(int age);
  17. void setHome(string home);
  18. };
  19. Student::Student(string name, int age, string home){
  20. this -> name = name;
  21. this -> age = age;
  22. this -> home = home;
  23. }
  24. string Student::getName(){
  25. return name;
  26. }
  27. int Student::getAge(){
  28. return age;
  29. }
  30. string Student::getHome(){
  31. return home;
  32. }
  33. void Student::setName(string name){
  34. this -> name = name;
  35. }
  36. void Student::setAge(int age){
  37. this -> age = age;
  38. }
  39. void Student::setHome(string home){
  40. this -> home = home;
  41. }
  42.  
  43. }
  44.  
  45. // 使用命名空间,方法1
  46. using namespace my_namespace;
  47. int main(){
  48. Student *stu1 = new Student(" 张三", 18, "北京");
  49. cout << stu1 -> getName() << endl;
  50. }
  51.  
  52. // 使用命名空间,方法2
  53. int main(){
  54. my_namespace::Student *stu1 = new my_namespace::Student(" 张三", 18, "北京");
  55. cout << stu1 -> getName() << endl;
  56. }
  57.  

总结

到此这篇关于C++对象与继承使用中一些问题介绍的文章就介绍到这了,更多相关C++对象与继承内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号