Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Computer program
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====C++==== In the 1970s, [[software engineers]] needed language support to break large projects down into [[Modular programming|modules]].<ref name="cpl_3rd-ch2-38">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 38 | isbn = 0-201-71012-9 }}</ref> One obvious feature was to decompose large projects ''physically'' into separate [[computer file|files]]. A less obvious feature was to decompose large projects ''logically'' into [[abstract data type]]s.<ref name="cpl_3rd-ch2-38"/> At the time, languages supported [[Type system|concrete (scalar)]] datatypes like [[integer]] numbers, [[floating-point]] numbers, and [[String (computer science)|strings]] of [[Character (computing)|characters]]. Abstract datatypes are [[Record (computer science)|structures]] of concrete datatypes, with a new name assigned. For example, a [[List (abstract data type)|list]] of integers could be called <code>integer_list</code>. In object-oriented jargon, abstract datatypes are called [[Class (computer programming)|classes]]. However, a ''class'' is only a definition; no memory is allocated. When memory is allocated to a class and [[Name binding|bound]] to an [[identifier]], it is called an [[Object (computer science)|object]].<ref name="cpl_3rd-ch8-193">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 193 | isbn = 0-201-71012-9 }}</ref> [[Object-oriented programming|Object-oriented imperative languages]] developed by combining the need for classes and the need for safe [[functional programming]].<ref name="cpl_3rd-ch2-39">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 39 | isbn = 0-201-71012-9 }}</ref> A function, in an object-oriented language, is assigned to a class. An assigned function is then referred to as a [[Method (computer programming)|method]], [[member function]], or [[Operation (mathematics)|operation]]. ''Object-oriented programming'' is executing ''operations'' on ''objects''.<ref name="cpl_3rd-ch2-35">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 35 | isbn = 0-201-71012-9 }}</ref> ''Object-oriented languages'' support a syntax to model [[subset|subset/superset]] relationships. In [[set theory]], an [[Element (mathematics)|element]] of a subset inherits all the attributes contained in the superset. For example, a student is a person. Therefore, the set of students is a subset of the set of persons. As a result, students inherit all the attributes common to all persons. Additionally, students have unique attributes that other people do not have. ''Object-oriented languages'' model ''subset/superset'' relationships using [[Inheritance (object-oriented programming)|inheritance]].<ref name="cpl_3rd-ch8-192">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 192 | isbn = 0-201-71012-9 }}</ref> ''Object-oriented programming'' became the dominant language paradigm by the late 1990s.<ref name="cpl_3rd-ch2-38"/> [[C++]] (1985) was originally called "C with Classes".<ref name="stroustrup-notes-22">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 22 | isbn = 978-0-321-56384-2 }}</ref> It was designed to expand [[C (programming language)|C's]] capabilities by adding the object-oriented facilities of the language [[Simula]].<ref name="stroustrup-notes-21">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 21 | isbn = 978-0-321-56384-2 }}</ref> An object-oriented module is composed of two files. The definitions file is called the [[header file]]. Here is a C++ ''header file'' for the ''GRADE class'' in a simple school application: <syntaxhighlight lang="cpp"> // grade.h // ------- // Used to allow multiple source files to include // this header file without duplication errors. // ---------------------------------------------- #ifndef GRADE_H #define GRADE_H class GRADE { public: // This is the constructor operation. // ---------------------------------- GRADE ( const char letter ); // This is a class variable. // ------------------------- char letter; // This is a member operation. // --------------------------- int grade_numeric( const char letter ); // This is a class variable. // ------------------------- int numeric; }; #endif </syntaxhighlight> A [[Constructor (object-oriented programming)|constructor]] operation is a function with the same name as the class name.<ref name="stroustrup-ch2-49">{{cite book | last = Stroustrup | first = Bjarne | title = The C++ Programming Language, Fourth Edition | publisher = Addison-Wesley | year = 2013 | page = 49 | isbn = 978-0-321-56384-2 }}</ref> It is executed when the calling operation executes the <code>[[new and delete (C++)|new]]</code> statement. A module's other file is the [[source file]]. Here is a C++ source file for the ''GRADE class'' in a simple school application: <syntaxhighlight lang="cpp"> // grade.cpp // --------- #include "grade.h" GRADE::GRADE( const char letter ) { // Reference the object using the keyword 'this'. // ---------------------------------------------- this->letter = letter; // This is Temporal Cohesion // ------------------------- this->numeric = grade_numeric( letter ); } int GRADE::grade_numeric( const char letter ) { if ( ( letter == 'A' || letter == 'a' ) ) return 4; else if ( ( letter == 'B' || letter == 'b' ) ) return 3; else if ( ( letter == 'C' || letter == 'c' ) ) return 2; else if ( ( letter == 'D' || letter == 'd' ) ) return 1; else if ( ( letter == 'F' || letter == 'f' ) ) return 0; else return -1; } </syntaxhighlight> Here is a C++ ''header file'' for the ''PERSON class'' in a simple school application: <syntaxhighlight lang="cpp"> // person.h // -------- #ifndef PERSON_H #define PERSON_H class PERSON { public: PERSON ( const char *name ); const char *name; }; #endif </syntaxhighlight> Here is a C++ ''source file'' for the ''PERSON class'' in a simple school application: <syntaxhighlight lang="cpp"> // person.cpp // ---------- #include "person.h" PERSON::PERSON ( const char *name ) { this->name = name; } </syntaxhighlight> Here is a C++ ''header file'' for the ''STUDENT class'' in a simple school application: <syntaxhighlight lang="cpp"> // student.h // --------- #ifndef STUDENT_H #define STUDENT_H #include "person.h" #include "grade.h" // A STUDENT is a subset of PERSON. // -------------------------------- class STUDENT : public PERSON{ public: STUDENT ( const char *name ); GRADE *grade; }; #endif </syntaxhighlight> Here is a C++ ''source file'' for the ''STUDENT class'' in a simple school application: <syntaxhighlight lang="cpp"> // student.cpp // ----------- #include "student.h" #include "person.h" STUDENT::STUDENT ( const char *name ): // Execute the constructor of the PERSON superclass. // ------------------------------------------------- PERSON( name ) { // Nothing else to do. // ------------------- } </syntaxhighlight> Here is a driver program for demonstration: <syntaxhighlight lang="cpp"> // student_dvr.cpp // --------------- #include <iostream> #include "student.h" int main( void ) { STUDENT *student = new STUDENT( "The Student" ); student->grade = new GRADE( 'a' ); std::cout // Notice student inherits PERSON's name << student->name << ": Numeric grade = " << student->grade->numeric << "\n"; return 0; } </syntaxhighlight> Here is a [[makefile]] to compile everything: <syntaxhighlight lang="make"> # makefile # -------- all: student_dvr clean: rm student_dvr *.o student_dvr: student_dvr.cpp grade.o student.o person.o c++ student_dvr.cpp grade.o student.o person.o -o student_dvr grade.o: grade.cpp grade.h c++ -c grade.cpp student.o: student.cpp student.h c++ -c student.cpp person.o: person.cpp person.h c++ -c person.cpp </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Computer program
(section)
Add topic