-
May 24th, 2003, 03:22 PM
#1
Member
OOP in C++
This tutorial gives the basics of Object Oriented Programming (OOP) in
C++. In programming, an object is used to group related data. In C++
this is done through classes. This tutorial assumes that the reader
has a basic knowledge of C++.
Class Basics
Here is the basic syntax of a class.
class Object
{
public:
//functions, variables, etc.
private:
//functions, variables, etc.
};
Data elements that are under public can be accessed outside of the
class but data under private cannot. Also, please note the required
semi-colon after the close bracket.
To create an instance of this object you would declare it like any
other data type:
Object object;
Let's look at an example:
class Person
{
public:
bool male;
int age;
bool GetMaritalStatus();
private:
bool single;
};
bool Person::GetMaritalStatus()
{
return single;
}
In OOP it is considered good style, as far as I know, for data under
public to be things that would be publicly accessed by anyone. In our
example, anyone should be able to tell if a person is male or not
(hopefully) and should also be able to roughly tell their age. Whether
a person is single or not, however, is usually not publicly available
so it is under private. The person may choose to release whether they
are single or not so there is a function in public, GetMaritalStatus,
that will return whether the person is single or not. This is called a
member function. Member functions are just like regular functions,
they can have parameters passed to them, output, anything that any
other function can do. Please note that you cannot initialize a variable for anything inside a
class without using a function. For example, we would get an error if,
when defining single, we wrote bool single=true. Note how the function is defined in the class but written outside of the class in the format of:
data type Class::Function().
This function also could have been written inside the class to look
like this:
public:
bool GetMaritalStatus()
{
return single;
}
It is proper style to write large functions outside of the class
because it is easier to read.
Constructors
If we were to create a person:
Person cire;
cire would habe no age or anything, this is where constructors come in.
They are member functions that are used to pass data to the class.
Contructors always use the same name of the class and do not have a
data type. Using our person example, here is how a constructor would
work:
class Person
{
public:
Person(int years, bool single, bool male);
bool male;
int age;
bool GetMaritalStatus();
private:
bool single;
};
Person::Person(int years, bool single, bool male)
{
age=years;
male=male;
single=single;
}
Now to create a Person, it would look like this:
Person cire(17,true,true);
To access a member function we would write:
cire.GetMaritalStatus();
which would return true.
Structures
There is another type of format for creating an object and that is
using a structure. The difference between a structure and a class is
that in a structure there is no private, every peice of data is public.
The structure for a structure (no pun intended) is:
struct Object
{
//variables, functions, etc.
};
This concludes this tutorial, but there is much more to C++ OOP
including inheritance which I may cover later. Hope this was
informative.
-
May 24th, 2003, 05:51 PM
#2
Cire, as much as your tutorial is well meant, it's slightly off-track about the OOP way-of-things.
Here are a few additions/corrections I'd like to add:
"In programming, an object is used to group related data. "
=>In programming, an object is used to group related data and its related functions into a single entity.
The further goal of objects is to encasulate the data so that it cannot be accessed externaly without passing by the member functions (setters/getters/buisiness logic fonctions...). The purpose of encapsulation is to protect the data from un-sound/illegal modifications as well as to give a simplified, unified interface to the data. This not only ensures that the data is always in a valid state (since you can control how it is modified) but it facilitates code reusability since external users of the class don't have to know the internal implementation of that class. A well desinged class is "paranoid": it doesn't trust the programmer that uses it.
"In OOP it is considered good style, as far as I know, for data under public to be things that would be publicly accessed by anyone"
=> Well, no, not really, at least not like that...:
You shouldn't try to litteraly impose real life concepts in your program designs; I had a self proclaimed "OOP" teacher this year who did that and he had just the WORTS OOP DESIGNS I had ever seen. Ok, this might not really be the case here so I wont go into it now (if someone wants to know what I meant, say it and I'll explain).
Ok, lets say your buisness model (ie: what you want your program to do) requires you to be able to verify the sex of a person. If you meet someone on the sidewalk and want to know there gender, would you: a) go ahead and feel them up? b) ask them politly and let them feel themselves up? Same thing here...
All your data should be private. Then you provide public methods to interrogate the data. These are called "getters" because we normally prefix them with "get" (ie: "getSex(), getAge()")
If you also need to have external users of the class modifiy internal data, you use "setters" (ie: setSex( bool bSex), setAge(int iAge)...)
This way you can control how the data is modified; for example, in setAge(int iAge), you can validate that iAge is greater than zero, or greater that the previous value of age (since you can't get youger AFAIK...yet!)
The keyword in OOP is "encapsulation".
Ammo
Credit travels up, blame travels down -- The Boss
-
May 24th, 2003, 06:59 PM
#3
Thank you both, I am currently trying to learn C++, so any other information, links, book titles you could give me would be awsome and very much appreceated.
-
March 19th, 2005, 11:13 AM
#4
Junior Member
How can I define the class "BOOK" in c++
-
March 19th, 2005, 03:22 PM
#5
Good Day,
This thread is quite old, notice the flashing date? Please try not to post on threads that are really old. But in response to your question:
How can I define the class "BOOK" in c++
// This is a user-defined type. What about the book do you want to define? Example: define the size of the book. Then a function to calculate the size of the book could look like this:
class Book
{
double width
double height
double thickness
double volume ( )
{
return width * height * thickness
}
};
Connection refused, try again later.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|