Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Simple OO demonstration in C++, comparison to Python

From last week's quick introduction to C++ (a private course - half a day added on to the end of a C Programming course - an example of how C++ implements objects for a newcomer to C++ who's seen some Python.

Firstly, in C++ you must predefine the API - the interface between the user and the class. And also the member variables and methods of each object type too:

  class train {
    public:
      train(char *, int, int) ;
      int getpeeps();
    private:
      int length;
      int seats; };


Methods are defined in a similar way. In C++, there's no need to declare the object variable as a parameter (in fact you must not do so), but in the other hand you need to declare the class name on the front of each method. And the constructor is a method with the same name as the class. So:

  train::train(char *dest, int length, int spc) {
    this->length = length;
    this->seats = spc; }
  int train::getpeeps() {
    return (int)(this->length * this->seats * 1.4); }


Now for the application. You can create objects in several ways in C++; I chose to use the new keyword as it allocates memory on the heap in a similar way to Python allocates memory to objects

  swindon = new train("Swindon",5,83);
  cheltenham = new train("Cheltenham Spa",1,75);


And I can access that data via an object method.

  cattle = swindon->getpeeps();
  cout << "can take " << cattle << " to Swindon" << endl;
  cattle = cheltenham->getpeeps();
  cout << "can take " << cattle << " to Cheltenham" << endl;


If you prefer the "." notation of Python, I could replace
  cattle = swindon->getpeeps();
with
  cattle = (*swindon).getpeeps();
but that gets messy.

Complete C++ source code [here].
(written 2013-07-01, updated 2013-07-15)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y106 - Object Oriented Python
  [477] Class, static and unbound variables - (2005-10-25)
  [834] Python makes University Challenge - (2006-08-15)
  [900] Python - function v method - (2006-10-20)
  [1306] Python class rattling around - (2007-08-16)
  [1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [2017] Python - a truly dynamic language - (2009-01-30)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
  [3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
  [3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
  [3878] From Structured to Object Oriented Programming. - (2012-10-02)
  [3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [4448] What is the difference between a function and a method? - (2015-03-04)
  [4591] From single block to structure and object oriented programming - (2015-12-02)
  [4650] Why populate object with values as you construct them? - (2016-02-18)
  [4721] When to check an object type - Python isinstance example - (2016-11-03)

C232 - C and C based languages - Defining and using classes in C++
  [2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
  [2578] Where are your objects stored in C++? - (2010-01-16)
  [2579] Creating, setting up and using objects in C++ - (2010-01-16)
  [3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
  [3716] Learning C++ - a design pattern for your first class - (2012-05-02)
  [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3978] Teaching OO - how to avoid lots of window switching early on - (2013-01-17)
  [4372] Template / design pattern for C++ constructor and accessors - (2014-12-29)
  [4565] Allocation of memory for objects in C++ - Stack v Heap - (2015-10-31)


Back to
Allocating memory dynamically in a static language like C
Previous and next
or
Horse's mouth home
Forward to
Soft furnishings up a Welsh Mountain
Some other Articles
In the hills above Harlech, and on the coast too
Using your own laptop on our courses - now even easier!
West Coast (of Wales) - railway and stations in pictures
Soft furnishings up a Welsh Mountain
Simple OO demonstration in C++, comparison to Python
Allocating memory dynamically in a static language like C
Exploring the area ... Ynys, near Harlech
Where are we now? On holiday!
The first Luas of the morning
Chippenham - Melksham - Trowbridge bus changes next month
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4129_Sim ... ython.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb