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
93 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to gracefully close the c++ 11 thread? 3 155
Embarcadero WebBroker REST server endpoint parameters 2 112
Why isn't object file created? 6 105
cmake and message 1 18
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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