C + + Language Basics Reference-can students add?

Source: Internet
Author: User
Tags class operator

Back to: Teacher He course teaching link



When it comes to arithmetic, we are always accustomed to the arithmetic of "number". In fact, objects like time are also capable of adding and subtraction operations. Further, our thinking can be expanded, and operations added to anything, as long as we can design the meaning of these "operations."

For example, there are class classes and student classes. Two students were added to form a class consisting of two students, and the class plus students added a new classmate on behalf of the class, and the students took the students, and the result was that the two students were "paired" and could do something (this pair, which needs to be defined as a new class); Class by class, all students in the class, So possible combinations of 22 knot pairs.

In fact, the class and students, have initially had the database in the "Student table" to store student information characteristics. A "student table" represents a class, and each "record" in this table is a specific student object.

Database technology can be used to build this table, you can manipulate the database language (SQL language), to complete the processing of data.

These processes are based on the database theory. For relational databases (the "Student table", in terms of terminology, is a "relationship"), based on "relational algebra". Relational algebra, in effect, defines a set of operations. Regardless of the notation for these operations, you can connect with the overloaded operators in C + +, and let's take a look at what strategies you can adopt to implement the lowest level of database operations in C + +.

In a deeper layer, an algebraic system is a system consisting of a non-empty set and a set of operations. This is any computational model (the computational model can be a generic model, and more of what we emphasize is to solve the specific problem of the computational pattern), in essence the description. Relational algebra is just an example of an algebraic system.

Therefore, the opportunity to "teach" students to learn computer, with the progress of the curriculum, with the help of discrete mathematics (algebra system is part of this course), computational theory and so on, experience from the most general, abstract aspects of computing, which is the formation of a professional thinking of the important part.

Of course, in the study of C + +, the experience of "All things are objects, all kinds of operations are operations", to get rid of the operation must be applied to the "number" on the concept, can help the future to learn the abstract theory of concrete up. It is necessary to study in a university to achieve theory and practice. To this end, the goal of this article is to let the rookie in contact with the professional theory, at least through the door to see one eye.

The following code to order the dry goods, students plus students of the operation, can be implemented as follows:

#include <iostream>using namespace Std;class Student;class class//class class {int number;    Student *students;    Friend Class Student;public:class ();    Class (const class &AMP;C); void display ();};    Class Student//Monk class {Private:int num;    String name;    char sex;    int age;    Friend Class Class;public:student () {} Student (int n, String nam, char s, int a); Class operator+ (Student &s1);};     Class::class (): Number (0), students (NULL) {}class::class (const Class &c) {number=c.number;     Students=new Student[number];     int i; for (i=0;i<number;i++) students[i]=c.students[i];}    void Class::d isplay () {int i; for (i=0; i<number; i++) {cout<<i<< ":" <<students[i].num<< "," <<students[i].nam        E    cout<< "," <<students[i].sex<< "," <<students[i].age<<endl; }}student::student (int n, String nam, char s, int a): num (n), name (NAM), sex (s), age (a) {}class student::operator+ (StuDent &s1) {Class C;   c.number=2;        C.students=new Student[c.number];        C.students[0]=*this;         C.STUDENTS[1]=S1; return c;}    int main () {Student S1 (1, "Zhang", ' f ', '), S2 (2, "Li", ' m ', 18);    Class C = s1+s2;    C.display (); return 0;}

Further expansion (invites interested readers as practical topics):

The definition of student S1, S2; Class C1, C2; After:

(1) Can you define operations that support C1+C2, C1+S1, C1-C2, and so on?

(2) to determine its meaning for s1*s2, s1*c1, C1*C2, add a new type definition if necessary, and implement the overloading of the * operator?

(3) (farther future) the definition of division in reference-relational algebra, implementing C1/S1, C1/C2 operations?





C + + Language Basics Reference-can students add?

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.