| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Phil Sykes" |
| Date: |
10 Sep 2003 06:01:17 AM |
| Object: |
object-oriented c++ newbie question |
the following three files are my first attempt at creating a class and
instantiating - their names explain what's what - my question is.....
is what i
have done in declarationsheader.h an example of abstraction,
encapsulation, or
data hiding...or none of these ? see below thanks, Phil.
// declarationsheader.h
class myclass // no base classes
{
private:
int var;
public:
int printdata(int var);
}; // no instances
--------------------------------------------------------
// definitionfile.cpp
#include"declarationsheader.h"
#include<iostream>
int myclass::printdata(int var)
{
return var;
}
--------------------------------------------------------
// main.cpp
#include"declarationsheader.h"
#include<iostream>
int main(int argc, char *argv[])
{
int myvalue;
std::cin >> myvalue;
myclass myinstance;
std::cout << "the value you entered was " <<
myinstance.printdata(myvalue) << std::endl;
std::cin.get();
std::cin.get();
return 0;
}
---------------------------------------------------------
.
|
|
| User: "jeffc" |
|
| Title: Re: object-oriented c++ newbie question |
10 Sep 2003 09:30:42 AM |
|
|
"Phil Sykes" <massiveattack92@hotmail.com> wrote in message
news:df2aa91c.0309100301.1cab6ff5@posting.google.com...
the following three files are my first attempt at creating a class and
instantiating - their names explain what's what - my question is.....
is what i
have done in declarationsheader.h an example of abstraction,
encapsulation, or
data hiding...or none of these ? see below thanks, Phil.
// declarationsheader.h
class myclass // no base classes
{
private:
int var;
public:
int printdata(int var);
}; // no instances
This comment makes no sense.
--------------------------------------------------------
// definitionfile.cpp
#include"declarationsheader.h"
#include<iostream>
int myclass::printdata(int var)
{
return var;
}
This is a bad situation. You've declared 2 different variables called
"var". Why? Technically, you have data hiding, since the "var" in
"myclass" is private. Technically you have encapsulation since you have
data and functions together in a class. Technically you have an
abstraction, because you have a class with an interface, but it's not a very
good interface. This might be better.
class myclass // no base classes
{
private:
int var;
public:
myclass(int newVar); // give it the value on the constructor
int printdata();
};
myclass::myclass(int newVar)
{
var = newVar;
}
// main.cpp
#include"declarationsheader.h"
#include<iostream>
int main() // you don't need any parameters here
{
int myvalue;
std::cin >> myvalue;
myclass myinstance(myvalue); // now the object has the value in it
// you don't need to pass
it to printdata
std::cout << "the value you entered was " << myinstance.printdata() <<
std::endl;
std::cin.get();
std::cin.get();
return 0;
}
Another option would be another function to set the value, rather than
providing it on the constructor. However, passing the value on printdata
basically looks like bad OO design.
.
|
|
|
|

|
Related Articles |
|
|