Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use struct and class of C++

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use C++ 's struct and class". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Keywords of the class

Struct already has its own meaning in C and must continue to be compatible.

A new keyword class is provided in C++ for class definition

The usage of class and struct is exactly the same.

When defining a class with struct, the default access level for all members is public

When defining a class with class, the default access level for all members is private

As follows:

Let's look at a piece of code that uses class:

# include struct A {/ / defualt to public int i; / / defualt to public int getI () {return I;}}; class B {/ / defualt to private int i; / / defualt to private int getI () {return I;}}; int main () {An a; B b; a.I = 4 Printf ("a.getI () =% d\ n", a.getI ()); B.I = 4; printf ("b.getI () =% d\ n", b.getI ()); return 0;}

The following is the output. Run an error indicating the class declared by class. The default access level for all members is private.

Add a public to class B, as follows:

Class B {public: / / defualt to private int i; / / defualt to private int getI () {return I;}}

The following is the output:

Second, the true form of the class

Classes in C++ support the separation of declaration and implementation

Separate the implementation and definition of a class

There are only class declarations in the .h header file

Declaration of member variables and member functions

Other implementations of completed classes in .cpp source files

The concrete realization of member function

III. Small examples

Requirements: develop a class for four operations

Provide setOperator function to set operation type

Provide setParameter function to set operation parameters

Provide result function for operation

Its return value indicates the validity of the operation.

Return the result by referencing the parameter

The code is as follows:

Operator.h

# ifndef _ OPERATOR_H_#define _ OPERATOR_H_class Operator {private: char mOp; double mP1; double mP2;public: bool setOperator (char op); void setParameter (double p1, double p2); bool result (double& r);}; # endif

Operator.cpp

# include "Operator.h" bool Operator::setOperator (char op) {bool ret = false; if ((op = ='+') | | (op = ='-') | | (op = ='*') | | (op = ='/') {ret = true; mOp = op;} else {mOp ='0;} return ret;} void Operator::setParameter (double p1, double p2) {mP1 = p1 MP2 = p2;} bool Operator::result (double& r) {bool ret = true; switch (mOp) {case'/': if ((- 0.000000001)

< mP2) && (mP2 < 0.000000001) ) { ret = false; } else { r = mP1 / mP2; } break; case '+': r = mP1 + mP2; break; case '*': r = mP1 * mP2; break; case '-': r = mP1 - mP2; break; default: ret = false; break; } return ret;} test.cpp #include #include "Operator.h" int main(){ Operator op; double r = 0; op.setOperator('/'); op.setParameter(9, 3); if( op.result(r) ) { printf("r = %lf\n", r); } else { printf("Calculate error!\n"); } return 0;} 下面为输出结果:

This is the end of the content of "how to use C++ 's struct and class". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report