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
71 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now