object-oriented c++ newbie question

P

Phil Sykes

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;
}

---------------------------------------------------------
 
J

jeffc

Phil Sykes said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top