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
92 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
  • 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

Industry Leaders: 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

685 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