c/c++语言开发共享C++对象与继承使用中一些问题介绍

目录定义抽象类类的继承使用new关键字实例化对象,只能用指针变量来接收使用普通的对象属性及方法时使用.来引用,但是用指针表示的对象则使用->定义类的时候,属性需要赋初始值的请况类的前置声明命名空

目录
  • 定义抽象类
  • 类的继承
  • 使用new关键字实例化对象,只能用指针变量来接收
  • 使用普通的对象属性及方法时使用.来引用,但是用指针表示的对象则使用->
  • 定义类的时候,属性需要赋初始值的请况
  • 类的前置声明
  • 命名空间的使用
  • 总结

定义抽象类

class person {  public:  	virtual void born() = 0;  };  

类的继承

class man :public person {  public:  	void born() {  		cout << " 出生了一个男人" << endl;  	}  };  

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

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

person* generatepersion(person* person1) {  	person* person = new man();  	retrun person;  }  

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

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

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

class studentid {  private:  	static studentid* si;  // 必须用static修饰  	static string id;  // 必须用static修饰  };  string studentid::id = "20200001";  studentid* studentid::si = null;  

类的前置声明

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

class abstractchatroom;  // 类的前置声明    class member{  protected:      abstractchatroom *chatroom;  // 使用之前必须先定义      void setchatroom(abstractchatroom *chatroom) {  // 类方法可以在类定义的时候就实现          this->chatroom = chatroom;      }      void chatroom_play();  // 当方法内部需要使用到前置声明类中的方法时,只能先定义,后实现  };    class abstractchatroom{  // 类的具体定义  public:  	member member;  // 类的相互嵌套      virtual void sendtext(string from,string to,string message) = 0;      void play(){  		cout << "在聊天室里玩耍!" << enld;  	}  };    void member::chatroom_play(){  // 类方法的具体实现  	chatroom -> play();  }  

命名空间的使用

#include <iostream>  using namespace std;    namespace my_namespace{  // 定义命名空间    class student{  // 命名空间中的对象  public:  	string name;  	int age;  	string home;  	student(string name, int age, string home);  	string getname();  	int getage();  	string gethome();  	void setname(string name);  	void setage(int age);  	void sethome(string home);  };  student::student(string name, int age, string home){      this -> name = name;      this -> age = age;      this -> home = home;  }  string student::getname(){      return name;  }  int student::getage(){      return age;  }  string student::gethome(){      return home;  }  void student::setname(string name){      this -> name = name;  }  void student::setage(int age){      this -> age = age;  }  void student::sethome(string home){      this -> home = home;  }    }    // 使用命名空间,方法1  using namespace my_namespace;  int main(){  	student *stu1 = new student(" 张三", 18, "北京");  	cout << stu1 -> getname() << endl;  }    // 使用命名空间,方法2  int main(){  	my_namespace::student *stu1 = new my_namespace::student(" 张三", 18, "北京");  	cout << stu1 -> getname() << endl;  }    

总结

到此这篇关于c++对象与继承使用中一些问题介绍的文章就介绍到这了,更多相关c++对象与继承内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享C++对象与继承使用中一些问题介绍,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1038428.html

(0)
上一篇 2022年1月26日
下一篇 2022年1月26日

精彩推荐