He teacher's teaching link this lesson explains
Example 1: Find the perimeter and area of a triangle
#include <iostream> #include <Cmath> #include <cstdlib> using namespace std; class Triangle {public:void setabc (double x, double y, double z)//Set three-side value, note to be able to be triangular double perimeter ();//compute the perimeter of the triangle do Uble area ();//calculate and return the size of the triangle private:double a,b,c;
Three sides for private member data}; int main () {triangle tri1;
Defines an instance of a triangle Class (object) Double x, y, Z;
cin>>x>>y>>z; TRI1.SETABC (x, y, z);
The perimeter of the triangle for the cout<< initial value is: "<< tri1.perimeter () <<", the area is: "<< tri1.area () <<endl;
return 0; Define each member function void Triangle::setabc (double x, double y, double z) {if (X+Y>Z&&X+Z>Y&&A) in the Triangle class below
mp;y+z>x) {a=x;
B=y;
C=z;
else {cout<< "cannot make a triangle" <<endl;
Exit (0);
Double triangle::p erimeter () {return a+b+c;}
Double Triangle::area () {double p= (a+b+c)/2;
return sqrt (p* (p-a) * (p-b) * (p-c)); }
Example 2: Student Information and performance management
#include <iostream> #include <cstring> using namespace std; Class Stu {Private:int no;//Student Study number char name[20]; Student name Double score[3]; Course score public:double average ()//Calculate average score double sum (); Calculate total void Show ();
Print information void setstudent (int, char*);/Enter student number, name void Inputscore ();
}; Double Stu::average () {return sum ()/3;} Average score Double stu::sum () {return score[0]+score[1]+score[2];}
Total void Stu::show ()//print info {cout<< "No.:" <<no<< ", Name:" <<name<<endl;
cout<< "Score:" <<score[0]<< "," <<score[1]<< "," <<score[2]<<endl;
cout<< "Average:" <<average () << ' t ' << "Sum:" <<sum () <<endl<<endl;} void Stu::setstudent (int n, char *nam) {no = n; School Number strcpy (Name,nam);
name} void Stu::inputscore () {int i;
cout<< "Please input score of" <<name<< ":"; for (i=0; i<3;i++) cin>>score[i];
int main () {Stu s1,*p2;
S1.setstudent ("Li Qing");
S1.inputscore ();
S1.show ();
P2 = new Stu; P2->setstudent ("Wang Gang");
Object Set initial value P2->inputscore ();
P2->show ();
return 0; }