[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

16. How to define method inside of a class? Outside of the class? What is the difference between these two definitions?

Posted on 2015-01-24
4
Medium Priority
?
107 Views
Last Modified: 2015-01-26
16. How to define method inside of a class? Outside of the class? What are the differences between these two definitions?
0
Comment
Question by:Nusrat Nuriyev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40568183
If you want to define methods inside a class, you would just add the implementation to the declaration like

class MyClass {
public:
    void foo(int x) {
        std::cout << "this is 'foo()' called with " << x << std::endl;
    }
};

Open in new window


This is convenient, but has one big disadvantage: Since you need the class declaration bein #included in other source files in order to use it, you will soon hit a situation when you get a 'method 'foo()' already defined when you include a header file that might already have incuded 'MyClass.h'. So, the better approach would be to split that like

//MyClass.h
class MyClass {
public:
    void foo(int x);
};

Open in new window

// MyClass.cpp
#include <iostream>
#include "MyClass.h"
    void MyClass::foo(int x) {
        std::cout << "this is 'foo()' called with " << x << std::endl;
    }

Open in new window

0
 

Author Comment

by:Nusrat Nuriyev
ID: 40569042
>>> you will soon hit a situation when you get a 'method 'foo()' already defined when you include a header file that might already have incuded 'MyClass.h'

1. Could you provide minimum real example?
2. Also what is the place of these preprocessor definitions  

#ifndef MYCLASS_INCLUDED
#define MYCLASS_INCLUDED

#endif // MYCLASS_INCLUDED

in OO paradigm?
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 40569146
OK, let me try to assemble one - seems harder to deliberately construct an error rather than running into it ;o)
But the one I used for 'polymorphism' is actually pretty good for that purpose:
// vehicle.h
class Vehicle {
public:
  virtual void Drive() {speed = 0;}
protected:
  int speed;
};

Open in new window

// bicycle.h
#include "vehicle.h"
class Bicycle: public Vehicle {
public:
  virtual void Drive() {speed = 10;}
};

Open in new window

// car.h
#include "vehicle.h"
class Car: public Vehicle {
public:
  virtual void Drive() {speed = 50;}
};

Open in new window

// main.cpp
#include "car.h" // will pull in vehicle.h with implementation code
#include "bicycle.h" // will pull in vehicle.h with implementation code as well -> boom!

void Race(Vehicle** vehicles, size_t count) {

  for (size_t i = 0; i < count; ++i) {

    vehicles[i]->Drive();
  }
}

int main () {

  Car car;
  Bicycle bike;

  Vehicle* arr[] = { &car, &bike};

  Race(arr, 2);

  return 0;
}

Open in new window

0
 

Author Comment

by:Nusrat Nuriyev
ID: 40570804
>>> seems harder to deliberately construct an error rather than running into it ;o)

Right! :) I'm faced with this constantly :)

Ok, so I have added implementations of
Vehicle::Drive() to Vehicle.cpp
Car::Drive()  to Car.cpp
Bicycle::Drive() to Bicycle.cpp

But I still have to embrace vehicle.h code with
#ifndef VEHICLE_H_INCLUDED
#define VEHICLE_H_INCLUDED
//...
#endif // VEHICLE_H_INCLUDED

Open in new window


As I had to add vehicle.h to all source files (except main.cpp)

I think it's ok.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question