A case study of C + + structural body usage _c language

Source: Internet
Author: User

This example describes the use of C + + architecture. Share to everyone for your reference. The specific analysis is as follows:

C + + architecture provides more functionality than C-struct, such as default constructors, copy constructors, operator overloads, which make it easy for struct objects to pass values.

For example, I define a simple structure and then use it as a vector element type, so you need to implement these three functions, otherwise you can only use pointers.

Copy Code code as follows:
#include <iostream>
#include <vector>
using namespace Std;
struct ST
int A;
int b;
ST ()//default constructor
A = 0;
b = 0;

void set (st* s1,st* s2)//Assignment function
S1->a = s2->a;
S1->b = s2->b;
st& operator= (const st& s)/overloaded operator
Set (this, (st*) &s)
ST (const st& s)//copy constructor
*this = s;
int main ()
ST A; Calling the default constructor
Vector<st> v;
V.push_back (a); Calling the copy constructor
ST s = v.at (0); Call = function
cout << S.A << "" << s.b << Endl;
Cin >> A.A;
return 0;

I hope this article will help you with the C + + program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.