Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Computer program
(section)
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!
===Object-oriented programming=== [[Object-oriented programming]] is a programming method to execute [[Method (computer programming)|operations]] ([[Function (computer programming)|functions]]) on [[Object (computer science)|objects]].<ref name="cpl_3rd-ch2-35_quote1">{{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 | quote = Simula was based on Algol 60 with one very important addition β the class concept. ... The basic idea was that the data (or data structure) and the operations performed on it belong together[.] }}</ref> The basic idea is to group the characteristics of a [[phenomenon]] into an object [[Record (computer science)|container]] and give the container a name. The ''operations'' on the phenomenon are also grouped into the container.<ref name="cpl_3rd-ch2-35_quote1"/> ''Object-oriented programming'' developed by combining the need for containers and the need for safe [[functional programming]].<ref name="cpl_3rd-ch2-39_quote1">{{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 | quote = Originally, a large number of experimental languages were designed, many of which combined object-oriented and functional programming. }}</ref> This programming method need not be confined to an ''object-oriented language''.<ref name="se-ch9-284_quote1">{{cite book | last = Schach | first = Stephen R. | title = Software Engineering | publisher = Aksen Associates Incorporated Publishers | year = 1990 | page = 284 | isbn = 0-256-08515-3 | quote = While it is true that OOD [(object oriented design)] as such is not supported by the majority of popular languages, a large subset of OOD can be used. }}</ref> In an object-oriented language, an object container is called a [[Class (computer programming)|class]]. In a non-object-oriented language, a [[data structure]] (which is also known as a [[Record (computer science)|record]]) may become an object container. To turn a data structure into an object container, operations need to be written specifically for the structure. The resulting structure is called an [[abstract datatype]].<ref name="dsa-ch3-p57">{{cite book | last = Weiss | first = Mark Allen | title = Data Structures and Algorithm Analysis in C++ | publisher = Benjamin/Cummings Publishing Company, Inc. | year = 1994 | page = 57 | isbn = 0-8053-5443-3 }}</ref> However, [[Inheritance (object-oriented programming)|inheritance]] will be missing. Nonetheless, this shortcoming can be overcome. Here is a [[C programming language]] ''header file'' for the ''GRADE abstract datatype'' in a simple school application: <syntaxhighlight lang="c"> /* grade.h */ /* ------- */ /* Used to allow multiple source files to include */ /* this header file without duplication errors. */ /* ---------------------------------------------- */ #ifndef GRADE_H #define GRADE_H typedef struct { char letter; } GRADE; /* Constructor */ /* ----------- */ GRADE *grade_new( char letter ); int grade_numeric( char letter ); #endif </syntaxhighlight> The <code>grade_new()</code> function performs the same algorithm as the C++ [[Constructor (object-oriented programming)|constructor]] operation. Here is a C programming language ''[[source file]]'' for the ''GRADE abstract datatype'' in a simple school application: <syntaxhighlight lang="c"> /* grade.c */ /* ------- */ #include "grade.h" GRADE *grade_new( char letter ) { GRADE *grade; /* Allocate heap memory */ /* -------------------- */ if ( ! ( grade = calloc( 1, sizeof ( GRADE ) ) ) ) { fprintf(stderr, "ERROR in %s/%s/%d: calloc() returned empty.\n", __FILE__, __FUNCTION__, __LINE__ ); exit( 1 ); } grade->letter = letter; return grade; } int grade_numeric( 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> In the constructor, the function <code>calloc()</code> is used instead of <code>malloc()</code> because each memory cell will be set to zero. Here is a C programming language ''header file'' for the ''PERSON abstract datatype'' in a simple school application: <syntaxhighlight lang="cpp"> /* person.h */ /* -------- */ #ifndef PERSON_H #define PERSON_H typedef struct { char *name; } PERSON; /* Constructor */ /* ----------- */ PERSON *person_new( char *name ); #endif </syntaxhighlight> Here is a C programming language ''source file'' for the ''PERSON abstract datatype'' in a simple school application: <syntaxhighlight lang="cpp"> /* person.c */ /* -------- */ #include "person.h" PERSON *person_new( char *name ) { PERSON *person; if ( ! ( person = calloc( 1, sizeof ( PERSON ) ) ) ) { fprintf(stderr, "ERROR in %s/%s/%d: calloc() returned empty.\n", __FILE__, __FUNCTION__, __LINE__ ); exit( 1 ); } person->name = name; return person; } </syntaxhighlight> Here is a C programming language ''header file'' for the ''STUDENT abstract datatype'' in a simple school application: <syntaxhighlight lang="c"> /* student.h */ /* --------- */ #ifndef STUDENT_H #define STUDENT_H #include "person.h" #include "grade.h" typedef struct { /* A STUDENT is a subset of PERSON. */ /* -------------------------------- */ PERSON *person; GRADE *grade; } STUDENT; /* Constructor */ /* ----------- */ STUDENT *student_new( char *name ); #endif </syntaxhighlight> Here is a C programming language ''source file'' for the ''STUDENT abstract datatype'' in a simple school application: <syntaxhighlight lang="cpp"> /* student.c */ /* --------- */ #include "student.h" #include "person.h" STUDENT *student_new( char *name ) { STUDENT *student; if ( ! ( student = calloc( 1, sizeof ( STUDENT ) ) ) ) { fprintf(stderr, "ERROR in %s/%s/%d: calloc() returned empty.\n", __FILE__, __FUNCTION__, __LINE__ ); exit( 1 ); } /* Execute the constructor of the PERSON superclass. */ /* ------------------------------------------------- */ student->person = person_new( name ); return student; } </syntaxhighlight> Here is a driver program for demonstration: <syntaxhighlight lang="c"> /* student_dvr.c */ /* ------------- */ #include <stdio.h> #include "student.h" int main( void ) { STUDENT *student = student_new( "The Student" ); student->grade = grade_new( 'a' ); printf( "%s: Numeric grade = %d\n", /* Whereas a subset exists, inheritance does not. */ student->person->name, /* Functional programming is executing functions just-in-time (JIT) */ grade_numeric( student->grade->letter ) ); 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.c grade.o student.o person.o gcc student_dvr.c grade.o student.o person.o -o student_dvr grade.o: grade.c grade.h gcc -c grade.c student.o: student.c student.h gcc -c student.c person.o: person.c person.h gcc -c person.c </syntaxhighlight> The formal strategy to build object-oriented objects is to:<ref name="se-ch9-285">{{cite book | last = Schach | first = Stephen R. | title = Software Engineering | publisher = Aksen Associates Incorporated Publishers | year = 1990 | page = 285 | isbn = 0-256-08515-3 }}</ref> * Identify the objects. Most likely these will be nouns. * Identify each object's attributes. What helps to describe the object? * Identify each object's actions. Most likely these will be verbs. * Identify the relationships from object to object. Most likely these will be verbs. For example: * A person is a human identified by a name. * A grade is an achievement identified by a letter. * A student is a person who earns a grade.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)