C + +: overview

Source: Internet
Author: User

1. Basic input and output, using cin>> input input, using cout<< output

#include <iostream>using namespacestd;intMain () {inta,b,d,min; cout<<"Enter The numbers:"<<Endl; CIN>>a>>b; cout<<"min ="<< (min=a>b?b:a);  for(d=2;d <min;d++)    if((a%d) = =0) && ((b%d) = =0)) Break; if(d==min) {cout<<"no common denominators"<<Endl; return 0; } cout<<"The lowest common denominator is"<<d<<Endl; return 0;} 
Enter The numbers: 1 2  10

2. Using a reference, the reference is represented by the address symbol &, the reference is another alias to the variable or constant, or the same data

Reference as return value

/*Requirements: Return function values using references*/#include<iostream>using namespacestd;int&f (int&i)//The definition function returns a reference to an integer type equivalent to the return value I{i+=Ten; returni;} intMain () {intk=0; int&m=f (k);//k=i=10cout<<k<<Endl; M= -; cout<<k<<Endl; return 0;}
Ten  -  0

References as parameters

/*Requirement: Reference as function parameter*/#include<iostream>using namespacestd;voidFint&m,intN) { inttemp; temp.=m; M=N; n=temp;}intMain () {intA =5, B =Ten; cout<<"A ="<<a<<" "<<"B ="<<b<<Endl; F (A, b); cout<<"A ="<<a<<" "<<"B ="<<b<<Endl;return 0;} 
5 Ten   ten 0

3. Using the scope Operator::, a function variable or a valid region of a constant or function

/*Requirements: Scope operators::*/#include<iostream>using namespacestd;intI= the;intMain () {inti; I= -;:: I= i+1; cout<<i<<Endl; cout<<::i<<Endl;return 0; }
 - 101  0

4. Simple use of structs, which have their own attribute members and method members, need to create struct member variables to invoke their own properties and methods

//#include <iostream>//#include <cmath>//using namespace std;#include <iostream.h>#include<math.h>structComplex//declares a struct named complex{ DoubleReal//data members, real parts of complex numbers DoubleImag//data members, imaginary parts of complex numbers voidInitDoubleRDoubleI//member function init, assigning the initial value to real and imag{Real=R; Imag=i;} DoubleAbscomplex ()//member function to find the absolute value of a complex number {  DoubleT; T= real*real+imag*Imag; returnsqrt (t);}}; intMain () {Complex A; //defines the member variable A of the structure body complexA.init (1.1,2.2);//Call member function init, assign the initial value to real and imagcout<<"the absolute value of a complex number is:"<<a.abscomplex () <<endl;//Call member function Abscomplex  return 0; }
The absolute value of the complex number is:2.459670

C + +: overview

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.