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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 119
Converting to C# also VS2015 5 117
Websocket through haproxy reused connection causes "Not a valid frame" error. 4 72
Least Squares Curve Fitting 4 58
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
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 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.

932 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

13 Experts available now in Live!

Get 1:1 Help Now