C + + self-study notes four times

Source: Internet
Author: User
Tags float max

Common methods of data sharing global variables have great limitations and undermine encapsulation. Static members, as a data member of a class, can implement data sharing between multiple objects, and the use of static data members does not return the principle of concealing information, guaranteeing the security of the program.
A static member belongs to a class, and if a static member is defined in a class, then each object can manipulate it.
#include <iostream>


using namespace Std;
Class ck{
Double Cunkuan; Deposit
Public
static double Lixi; Defining static data members Lixi
CK (double);
static void Modlixi (double);//define static member function Modlixi ()
void Callixi (int m=1);
void set (Double x) {//Set deposit amount
Cunkuan=x;
}
};


void Ck::callixi (int m) {
Double x=0.0;
X=CUNKUAN*LIXI/12; Calculate monthly Interest
Cunkuan+=x*m; Add interest to your deposit
cout<<cunkuan<<endl;
}


void Ck::modlixi (Double x) {//change annual interest rate

Lixi=x;
}


Ck::ck (double c) {
constructor function
Cunkuan=c;

}
Static variables must be initialized
Double ck::lixi=0;

int main (int argc, char** argv) {

CK Saver1 (2000.0), Saver2 (3000.0);//Instantiate two objects

Ck::modlixi (0.03);

cout<< "annual interest is 3%" <<endl;
cout<< "One months later, a deposit balance is:" <<endl;
Saver1.callixi ();
cout<< "One months after the deposit balance of B:" <<endl;
Saver2.callixi ();
Saver1.set (2000.0);
Saver2.set (3000.0);
cout<< "Three months later, a deposit balance is:" <<endl;
Saver1.callixi (3);
cout<< "Three months later, a deposit balance is:" <<endl;
Saver2.callixi (3);
return 0;
}




Friend, because the class has the characteristics of encapsulation and information hiding, so that its data members can not be accessed by other member functions, friend is to solve how to let a function defined outside the class to access the class's private members of the problem.


#include <iostream>


using namespace Std;


Class integer{
int n;
Public
Integer (int x=0) {
N=x;
}
friend int sum (Integer &a,integer &b);
};
int sum (Integer &a,integer &b) {
return A.N+B.N;
}
int main () {
Integer x (+), Y (37);
The result of cout<< "sum (x, y) is:" <<sum (x, y) <<endl;
return 0;
}






#include <iostream>


using namespace Std;


Class integer{
int n;
Public
Integer (int x=0) {
N=x;
}
Friend class operation; DECLARE friend class
};


Class operation{
Public:
int sum (integer &x, integer &y) {
return X.N+Y.N;
}
int substract (integer &x, integer &y) {
return X.N-Y.N;
}
int multilication (integer &x, integer &y) {
return X.N*Y.N;
}
int division (integer &x, integer &y) {
return X.N/Y.N;
}
};


int main () {
Integer x (in), Y (12);
Operation Z;
The result of cout<< "sum (x, y) is:" <<z.sum (x, y) <<endl;
The result of cout<< "substract (x, y) is:" <<z.substract (x, y) <<endl;
The result of cout<< "multilication (x, y) is:" <<z.multilication (x, y) <<endl;
The result of cout<< "division (x, y) is:" <<z.division (x, y) <<endl;
return 0;
}






Class operation is defined as a friend function of class integer, and class operation as a friend of class integer, all of its member functions will be friend functions of class integer. This means that all member functions of the class operation have access to the private members of the integer.




#include <iostream>


using namespace Std;


Class trigon{
float A,b,c;
Public
Trigon (float x,float y,float z) {
A=x;
B=y;
C=z;
}
Friend float sum (Trigon &tri);
};
Float sum (Trigon &tri) {
return TRI.A+TRI.B+TRI.C;
}


int main () {
Trigon Tri (3,4,5);
cout<< "Circumference of the triangle with the edge length 3,4,5:" <<sum (TRI) <<endl;

return 0;
}




#include <iostream>


using namespace Std;


Class trigon{
float A,b,c;
Public
Trigon (float x,float y,float z) {
A=x;
B=y;
C=z;
}
Friend class S;
};
Class s{
Public
Float sum (Trigon &tri) {

return TRI.A+TRI.B+TRI.C;
}
Float average (Trigon &tri) {
Return (TRI.A+TRI.B+TRI.C)/3.0;
}
Float max (Trigon &tri) {
int M=TRI.A;
if (M&LT;TRI.B)
M=TRI.B;
if (M&LT;TRI.C)
M=TRI.C;
return m;
}
};




int main () {
Trigon Tri (3,4,5);
S Z;
cout<< "Three numbers of 3,4,5" and: "<<z.sum (TRI) <<endl;
cout<< "average of three number 3,4,5" <<z.average (TRI) <<endl;
cout<< "Maximum of three number 3,4,5" <<z.max (TRI) <<endl;
return 0;
}




Polymorphic and virtual functions, polymorphism can be simply summed up as an interface, multiple methods. Virtual function is the basis of dynamic linking, always first with their own, how to live without, ask your father to.
Pure virtual functions only function declarations, but no functions are implemented specifically. The pure virtual function has no function body, and the specific function is implemented in the derived class.




Abstract classes that contain pure virtual functions are abstract classes, and abstract classes cannot produce objects.


Defines a figure class that contains a pure virtual function area () that represents the plot size.


#include <iostream>


using namespace Std;


Class figure{
Public
virtual void area () = 0; Pure virtual function Area interface
};


Class Rect:public figure{
Private
int A, B;
Public
Rect (int x,int y) {
A=x;
B=y;
}
void Area () {
cout<< "The area of the rectangle is" <<a*b<<endl;
}
};
Class Circle:public figure{
Double R;
Public
Circle (double x=1) {
R=x;

}
void Area () {
cout<< "The area of the circle is" <<r*r*3.141<<endl;
}

};
int main () {
Rect rect (9,8);
Circle Circle (2);
Figure *p;
p=&rect;
P->area ();
p=&circle;
P->area ();
return 0;
}






Polymorphism, inheritance, and overloading constitute the three major programming features of object-oriented, and the polymorphism is realized by virtual function.






#include <iostream>


using namespace Std;


int main () {


Char *str= "Hello world!";
Cout.put (3);
Cout.put (3);
Cout.put (3);
Cout.write (str,10);
Cout.put (' a ');//put is inserting a single character
Cout.flush ();
return 0;
}


The member functions of CIN Get () and getline () both have 3 parameters; they encounter a Terminator, which defaults to \ n


Getline () can extract Terminator


#include <iostream>


using namespace Std;


int main () {


cout<< "Please enter string:" <<endl;
Char buf1[30],buf2[30];
/* Cin.get (buf1,30);
cout<<buf1<<endl;
Cin.get (buf1,30);
cout<<buf1<<endl;*/

Cin.getline (buf2,30);
cout<<buf2<<endl;
Cin.getline (buf2,30);
cout<<buf2<<endl;
return 0;
}





















































C + + self-study notes four times

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.