C + + class and object instance Resolution (ii) _c language

Source: Internet
Author: User
Tags getdate

C + + is both object-oriented and process-oriented language, where there is an important concept- class .
What is a class? A class is an abstraction of an object, for example: every real person is an object, and people have a lot of common characteristics (a head, two legs, can walk, can run), and this person with common characteristics becomes a class. A class is an abstract noun, and each person (that is, an object) is an instance of this class.
The common characteristics between objects are the properties and behavior of objects. VCR is an object, its property is the manufacturer, brand, weight, color and so on, its behavior is its function, such as video, like, fast-forward, backwards and so on.
C + + programs, you need to first define a class, like this:
Class name
{
define variables;
....
Defining functions
}
A list of members in a class body, including functions for data and manipulating data, i.e. data members, member functions. This embodies the encapsulation of class and the concealment of information.
Member functions are generally declared in the class body, defined outside the class body.
Constructors: Assigning values to an object
destructor: Frees the space of an object member at the end of the function domain.
Inline function: To save time for a function call
Complete a variety of functional functions ....
Defining an object requires instantiating the class. Form: Class Name Object name

A class is an abstraction that does not occupy any memory and allocates space only after it is instantiated as an object.
The computer is the object to describe the universe of things. Objects are contacted through messages, and functions are accomplished with a function.
Simply put, this is the object and class.

The following examples understand

"2-1" defines a date for the C + + Class and Object period class, which holds a date, which can be provided with a date value, a year, a month, an extraction date, and a design date value.
"Analysis" the program needs to first define a class type, the data member of this class has year,month,day, and it is designed as private access rights. You also need to have 5 member functions, respectively:

 void getdate ();          Takes the date value, the format is "year-month-day" int getyear ();         Take the year int getmonth ();          Take the month int getday ();    Takes the date void setdate (int y,int m,int D);
Set Date value program code as follows: #include <iostream> using namespace std;
 Class Date {public:void getdate ();
 int getyear ();
 int getmonth ();
 int getday ();
void setdate (int y,int m,int D);
Private:int Year,month,day;
}; int Date::getyear () {return year;} int date::getmonth () {return month.} int Date::getday () {return day;} void Date::get Date () {cout<< ' is: ' <<year<< '-' <<month<< '-' <<day<<endl;} void Date:  : setdate (int y,int m,int d) {year=y; month=m; day=d;
 int main () {Date D;
 D.setdate (2011,2,1);
 cout<< "year=" <<d.getyear () <<endl;
 cout<< "Month=" <<d.getmonth () <<endl;
 cout<< "day=" <<d.getday () <<endl;
 D.getdate ();
return 0; }

"Summary" This is one of the simplest class procedures, but there are many problems. For example, without the setdate () function, you cannot assign an initial value to an object, and you must set an initial value on the object in a specific format. This problem should be based on constructors for object initialization.
"2-2" for example "2-1", which requires the use of constructors for object initialization, and the format is inconsistent.
The example of "analysis" requires the use of constructors and the use of constructor overloads, constructors with default parameter values, and copy constructors.
The program code is as follows:

#include <iostream>
using namespace std;
Class Date
{public
:
 date (int y=0,int m=0,int d=0)
 {year=y; month=m; day=d;}
 Date (date &d)
 {year=d.year; month=d.month; day=d.day;}
 void GetDate ()
 {cout<< "Today is:" <<year<< "-" <<month<< "-" <<day<< Endl
 int getyear () {return year;}
 int getmonth () {return month;}
 int Getday () {return day;}
Private:
 int year,month,day;
};

int main ()
{
 date D1 (2011,2,1), d2,d3 (D1);
 D1.getdate ();
 D2.getdate ();
 D3.getdate ();
 return 0;
}

The D1 of an object in the

Summary program has a complete argument list, the constructor is called to initialize the object, the object D2 has no argument list, and the system automatically calls the constructor for object initialization, except that the value of the formal parameter is initialized with the default argument value. For object D3, the copy constructor is used for initialization. The
"2-3" enters data into the array summation, maximum, minimum, and average output, and defines the member function outside the class. A summary of what is learned in C language when
analyzes this program is a process-oriented approach to object-oriented leapfrogging. The data members of this class should include an array. A member function should have an array input function, an array output function, a sum function, a maximum function, a minimum function, and an average function.
Program code is as follows:

#include <iostream> using namespace std;
 #define N Ten class Array {public:void input ();
 void output ();
 int Max ();
 int min ();
 int sum ();
float average ();
 Private:int A[n],maxnumber,minnumber,sumnumber;
float Avenumber;
};
 void Array::input () {int i=0;
 cout<< "Please input" <<N<< "numbers:" <<endl;
 for (i=0;i<n;i++) {cout<< "a[" <<i<< "]=";
 cin>>a[i];
 } void Array::output () {int i=0;
 cout<< "Array A is:" <<endl;
 for (i=0;i<n;i++) cout<<a[i]<< "";
cout<<endl;
 int Array::max () {int i;
 MAXNUMBER=A[0];
 for (i=1;i<n;i++) if (Maxnumber<a[i]) maxnumber=a[i];
return maxnumber;
 int array::min () {int i;
 MINNUMBER=A[0];
 for (i=1;i<n;i++) if (Minnumber>a[i]) minnumber=a[i];
return minnumber;
 int array::sum () {int i;
 SUMNUMBER=A[0];
 for (i=1;i<n;i++) sumnumber+=a[i];
return sumnumber; Float Array::average () {Float ave=static_cast<float> (Sumnumber/n);
Return Ave;
 int main () {array A;
 A.input ();
 A.output ();
 cout<< "The max number is" <<a.max () <<endl;
 cout<< "The min number is" <<a.min () <<endl;
 cout<< "The sum number is" <<a.sum () <<endl;
  cout<< "The average number is" <<a.average () <<endl;
return 0;
 }

Summary from the above procedure, you can see that the class array includes various operations of the array, including summation, maximum value, minimum value, and average.
"2-4" Design a cat's class, including cat color, weight, age and other data, with the setting of the cat color, modify and display the cat's weight, age and other operations. The
Analysis Design Cat class name is cat, this class has 3 data members, the color is expressed in string class, can store Chinese characters, the weight is expressed by the real type, the age is expressed by the integer type, in order to protect the data security, the 3 data members are all private. The setting and modification of the cat's attribute data are all public functions.
Program code is as follows:

#include <iostream>
#include <string>
using namespace std;
Class Cat
{public
:
 Cat (String c= "red", float w=1,int a=1);
 String Get_color ();
 float get_weight ();
 int get_age ();
 void display ();
Private:
 string color;
 float weight;
 int age;
};
Cat::cat (String c,float w,int a)
{
 color=c;
 Weight=w;
 age=a;
}
String Cat::get_color ()
{return
 color;
}
Float Cat::get_weight ()
{return
 weight;
}
int Cat::get_age ()
{return age
 ;
}
void Cat::d isplay ()
{
 cout<< "The color of this Cat is" <<get_color () <<endl;
 cout<< "The weight of this cat is" <<get_weight () <<endl;
 cout<< "The age of this cat is" <<get_age () <<endl;
}
int main ()
{
 Cat c1,c2 ("yellow", 1,2);
 cout<< "C1 is:" <<endl;
 C1.display ();
 cout<< "C2 is:" <<endl;
 C2.display ();
 return 0;
}

Summary from the above program, you can see that constructors can initialize objects with classes, and constructors can overload them.

I hope this article can help you learn the C + + programming language, and hope you can continue to pay attention!

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.