Introduction to zero basic C + + (10) object oriented

introduction zero basic object oriented


One 、 Learning goals

  1. understand C++ What is the class
  2. Understand that class objects are created just like type modifiers
  3. Learned how to use the properties of a class
  4. Understand the use of class methods

Two 、 Understand how to use classes

2.1 What is? C++ Class and basic usage Class is used to describe a collection of objects with the same properties and methods . It defines the properties and methods that are common to each object in the collection . Object is an instance of a class .

for example : People are a type of , Age 、 height 、 weight 、 Gender and other attributes . A specific person is an object , For example, Xiao Ming ; Xiao Ming has age 、 height 、 weight 、 Gender and other attributes . Xiaohong is also a human being , So it has the same properties .

Class variables class variables are common throughout the instantiated object . Or it can be understood as an attribute . Create a person's class , There are class variables in a class , Use class variables to record a person's age 、 Gender 、 Height and other attributes . Define a class using keywords class, Then there's a class name , Write the content of the current class in curly brackets after the class name :

class HumanClass{
public:
char name;
char sex;
int age;
};

The above code creates a type , This type has name name 、sex Gender and age The attributes of age , And these attributes are public ; Just like you play games , The name of the game character is visible , You can also see whether it's male or female ; among public It means a colon : After the definition of variables are public content . When we use these public properties , You can create an object directly and use it externally . The specific use is as follows :

#include<iostream>
using namespace std;
class HumanClass{
public:
char name;
char sex;
int age;
};
int main() {
HumanClass xiaoming;
xiaoming.age=11;
xiaoming.sex='N';
xiaoming.name='M';
cout<<" Xiaoming age yes :"<<xiaoming.age<<endl;
cout<<" Xiaoming sex yes :"<<xiaoming.sex<<endl;
cout<<" Xiaoming name yes :"<<xiaoming.name<<endl;
}

The above code to create a specific type is very simple , Use HumanClass xiaoming; I can just create one HumanClass This type of object , In this case, it is xiaoming; That means ,xiaoming Belong to this category , because age、sex、name All belong to the public property , Then we can change him , So the next step is to start talking about xiaoming This type of property is assigned a value :

xiaoming.age=11;
xiaoming.sex='N';
xiaoming.name='M';

Then we can output :

cout<<" Xiaoming age yes :"<<xiaoming.age<<endl;
cout<<" Xiaoming sex yes :"<<xiaoming.sex<<endl;
cout<<" Xiaoming name yes :"<<xiaoming.name<<endl;

Run the program to see the results :

2.2 Understand the use of class methods Class method refers to the function defined in this class , What functions does this class have ; One way for dogs, for example, is to make sounds , bark ; Set a way to output barks , Simulate the barking of a dog . Here is a class , be known as Dog:

class Dog{
public:
char name;
char sex;
int age;
void jiao(){
cout<<" bark "<<endl;
}
};

In the above class, a function , be known as jiao, This jiao The function of is output “ bark ”. We first create the object of this class :

Dog wang;

Then define the attribute value :

wang.age=11;
wang.sex='N';
wang.name='M';

Class is also very simple to use , The function is the same as the function after using it :

wang.jiao();

Finally, output the content :

cout<<" Xiaoming age yes :"<<wang.age<<endl;
cout<<" Xiaoming sex yes :"<<wang.sex<<endl;
cout<<" Xiaoming name yes :"<<wang.name<<endl;

The complete code is as follows :

#include<iostream>
using namespace std;
class Dog{
public:
char name;
char sex;
int age;
void jiao(){
cout<<" bark "<<endl;
}
};
int main() {
Dog wang;
wang.age=11;
wang.sex='N';
wang.name='M';
wang.jiao();
cout<<" Xiaoming age yes :"<<wang.age<<endl;
cout<<" Xiaoming sex yes :"<<wang.sex<<endl;
cout<<" Xiaoming name yes :"<<wang.name<<endl;
}

give the result as follows :

3、 ... and 、 summary

  1. understand C++ What is the class
  2. Understand that class objects are created just like type modifiers
  3. Learned how to use the properties of a class
  4. Understand the use of class methods

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[The official account of the green chicken in soya sauce]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210114174431038n.html

  1. Learn about RPC, why RPC was born, and what's the difference between RPC and HTTP?
  2. Learn about RPC, why RPC was born, and what's the difference between RPC and HTTP?
  3. Learn java base conversion supplementary learning
  4. JDBC测试连接数据库
  5. JDBC test connection database
  6. 大厂面试官竟然这么爱问Kafka,一连八个Kafka问题把我问蒙了?
  7. The interviewers of big factories love to ask Kafka so much. I'm blinded by eight Kafka questions in a row?
  8. 安卓开发和java开发有什么区别!2021年BATJ30套大厂Android经典高频面试题,面试必问
  9. Spring Security OAuth2.0認證授權四:分散式系統認證授權
  10. What's the difference between Android development and java development! 2021 batj30 Android classic high frequency interview questions
  11. Spring security oauth2.0 authentication and authorization 4: distributed system authentication and authorization
  12. Java微服务 vs Go微服务,究竟谁更强!?
  13. 大厂面试官竟然这么爱问Kafka,一连八个Kafka问题把我问蒙了?
  14. Who is stronger, Java microservice vs go microservice!?
  15. Java微服务 vs Go微服务,究竟谁更强!?
  16. The interviewers of big factories love to ask Kafka so much. I'm blinded by eight Kafka questions in a row?
  17. Who is stronger, Java microservice vs go microservice!?
  18. springboot异常处理之404
  19. Spring boot exception handling 404
  20. Spring Boot Security 国际化 多语言 i18n 趟过巨坑
  21. springboot异常处理之404
  22. Spring boot security international multilingual I18N
  23. Spring boot exception handling 404
  24. Netty系列化之Google Protobuf编解码
  25. Netty之编解码
  26. Java编解码
  27. Netty解码器
  28. Netty与TCP粘包拆包
  29. Netty开发入门
  30. Java集合遍历时遇到的坑
  31. Spring IOC 源码解析(下)
  32. Spring IoC源码解析(上)
  33. Google protobuf codec of netty serialization
  34. Encoding and decoding of netty
  35. Java codec
  36. Netty decoder
  37. Netty and TCP packet sticking and unpacking
  38. Introduction to netty development
  39. Problems encountered in Java collection traversal
  40. Spring IOC source code analysis (2)
  41. Spring IOC source code analysis (Part one)
  42. 半小时用Spring Boot注解实现Redis分布式锁
  43. Implementing redis distributed lock with spring boot annotation in half an hour
  44. What should we do if we can't get tickets for Spring Festival transportation? You can solve this problem by using these ticket grabbing apps!
  45. 百度智能(文本识别),API传图OC代码与SDK使用
  46. springboot源码解析-管中窥豹系列之aware(六)
  47. Baidu intelligent (text recognition), API map, OC code and SDK
  48. Spring boot source code analysis
  49. springboot源码解析-管中窥豹系列之aware(六)
  50. 百度智能(文本识别),API传图OC代码与SDK使用
  51. Spring boot source code analysis
  52. Baidu intelligent (text recognition), API map, OC code and SDK
  53. Java学习笔记
  54. Java learning notes
  55. Sentry(v20.12.1) K8S 雲原生架構探索, SENTRY FOR JAVASCRIPT 手動捕獲事件基本用法
  56. 我的程式設計師之路:自學Java篇
  57. SpringBoot專案,如何優雅的把介面引數中的空白值替換為null值?
  58. Sentry (v20.12.1) k8s cloud native architecture exploration, sentry for JavaScript manual capture event basic usage
  59. My way of programmer: self study java
  60. Spring boot project, how to gracefully replace the blank value in the interface argument with null value?