C + + language Basic routine class and object simple application example __c++

Source: Internet
Author: User

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; }


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.