All of the previous lessons up to this point have one thing in common — they have been non-object-oriented. Now that you have a basic handle on those concepts, we can proceed into object-oriented programming (OOP), where the real payoff is!
In traditional programming, programs are basically lists of instructions to the computer that define data and then work with that data. Data and the functions that work on that data are separate entities that are combined together to produce the desired result.
So what is object-oriented programming? As with so many things, it is perhaps understood most easily through use of an analogy. Take a look around you — everywhere you look are objects. Most objects have two major components to them: 1) A list of properties (eg. weight, color, size, texture, etc…), and 2) Some number of actions that either they can perform, or that can be performed on them (eg. being opened, having something poured into it, etc…). These two components are inseparable.
With traditional programming, the properties (data) and actions (functions) are separate entities, which means that traditional programming often does not provide a very intuitive representation of reality. We are intuitively used to thinking about things as objects, and expect to be able to perform actions with or on those objects.
Object-oriented programming (OOP) provides us with the ability to design “objects” that have both characteristics (sometimes called attributes, fields, or properties) and behaviors (methods or features), all tied together in one package. This allows programs to be written in a more modular fashion, which makes them easier to write and understand, and also provides a higher degree of code-reusability. Objects provide a more intuitive way to work with our data by allowing us to define how we interact with the objects, and how they interact with other objects. Object-oriented programming also brings several other useful concepts to the table: inheritance, encapsulation, abstraction, and polymorphism (language designers have a philosophy: never use a small word where a big one will do).
We will be covering all of these concepts in the upcoming tutorials over the next few chapters. It’s a lot of new material, but once you’ve been properly familiarized with OOP, you’ll never want to go back to traditional programming again.
8.2 — Classes and class members
|
Index
|
7.14 — Ellipses (and why to avoid them)
|
8.2 — Classes and class members
Index
7.14 — Ellipses (and why to avoid them)
I’m thrilled after I read this article!! At this moment I’m taking intermediate C++ programming, and soon our teacher will teach us object-oriented programming (hope I’m right). And again, thank you for creating such a great C++ resource for programmers
[...] 8.1 — Welcome to object-oriented programming [...]
So object oriented programming can only be done with C++…not java or anything other?
Object oriented programming is available in many of the popular languages today, including Java, C#, Python, Perl, etc…
Does the date at the top of the page tell when you wrote the page and published it?
[ Yes, that is correct. -Alex ]
I have been rather confused by some of the terminology of OOP, such as ‘class’, and ‘wrapper’, etc.; however, it appears as though these things are simply evolutions of the old ‘subroutine’ construct. Using a subroutine is now handled by the language which acts as a ‘traffic controller’ making it unnecessary for the programmer to keep up with ‘returns’ and allowing program flow to continue in any direction. If this is the case then it is not so hard to understand.
Its very nice material..
Dear all friends,
I am a beginner with C++. Actually it’s very difficult for me to complete exercises OPP at the begining. My teacher gave us some exercises. I have tried but not success. So, please help me on these exercises.
Exercise 1:(3Points)
The 10 digit International Standard Book Number(ISBN)is a unique numeric commercial book
identifier developed by the International Organization for Standardization(ISO).It was used until
2007,after which a 13 digit ISBN number was defined.
The 10 digit ISBN consists of 9 identifier digits and one check digit at the end.It is usually written
in the following form
X-XXX-XXXXX-C
where C denotes the check digit. The identifier digits are in the range 0-9,the check digit can be
either in the range 0-9 or be the letter ‚X’.
You should model an ISBN number as a C++ class, therefore:
• Draw a UML class diagram of the ISBN class(model the 9 identifier digits as one single
integer value with nine digits (e.g.0-233-45678 is modeled as the integer 23345678) ,the
check digit as a single character value)
• Declare your modeled ISBN class in C++, including a constructor that takes an integer and a
Character to initialize the corresponding member values, in a file named„ isbn.h“
• Implement the constructor of the class in a file named „isbn.cpp“. The constructor should
Initialize the member values of the object using the given parameter values.
Seehttp://en.wikipedia.org/wiki/ISBN#ISBN10foracloserexplanationoftheISBN10.
Exercise 2:(2Points)
Add a member function
void show () const;
to the ISBN class which prints the whole ISBN number (including check digit) on the screen in the following format:
X-XXX-XXXXX-C
If the number is shorter than 9 digits, fill it up with 0s to the left.
Explain in comments what the keyword„ const“ means in this case (void show () const)and why it is good (or not good) to be used for this function.
Exercise 3:(1Point)
Write a main function in a file called„sheet1.cpp“ which creates three ISBN objects (enter their
ISBN number as integer and check character directly into the C++source, you do not need to read
Them from std::cin) and prints out their numbers in the format shown in exercise 2.
Create a simple Make file and compile the program (consisting of the two.cpp implementation files
As well as the .h header file).Execute your program.
Thank you very much for your help.