Learntofish's Blog

A blog about math, physics and computer science

Posts Tagged ‘oop’

Python 3: Inheritance and super()

Posted by Ed on April 23, 2016

Inheritance in Object Oriented Programming (OOP) means that you create a new class by extending an existing class. For example, suppose we have a class Shape with the attribute position. Then we can create a new class Circle by taking Shape and extending it by a new attribute center: Read the rest of this entry »

Advertisements

Posted in computer science, programming | Tagged: , , | Leave a Comment »

Tutorial: OOP in C++ (Part 2 – Class and object)

Posted by Ed on October 6, 2012

This is part 2 of the C++ Object Oriented Programming tutorial. Last time we focused on using multiple files. Today we will have a look at classes and objects.

1. Definition of a class

Consider this example: Albert Einstein, Frank Sinatra, Marie Curie, what do they have in common? Obviously, they are persons. We say they are objects of the class “Person”. In general, a class describes its objects by two things:
1. Attributes (properties)
2. Methods (functions that can access attributes and change them)

The Person class above may have the attributes “name” and “age”. We will give it the method “talk()” which allows our objects to actually talk.

Let’s write the Person class in C++:
– Create a new project in Codelite and call it “personClass” by right-clicking on the orange “Tutorial” folder from last time. Then double click the “personClass” folder to set the project as active.
– Open the “main.cpp” file in the yellow “src” folder and replace the code by:

#include <iostream>
#include <string>
using namespace std;


// definition of class "Person"
class Person{

	// attributes	
	private:
		string name;
		int age;
   
	// methods
	public:
		Person(string myName, int myAge){
			name = myName;
			age = myAge;
		}

		void talk(){
			cout << "Hi! My name is " << name << " and I am " << age << " years old." << endl;
		}
};   // <--- Don't forget the semicolon here!


// main() function
int main(){

	Person p("Homer", 42);    // Create an object of the class "Person"
	p.talk();                 // Let the object talk
	
	return 0;
}

– In line 07-24 we defined Read the rest of this entry »

Posted in computer science | Tagged: , , , | Leave a Comment »

Tutorial: OOP in C++ (Part 1 – Multiple files)

Posted by Ed on October 3, 2012

This is part 1 of the tutorial on object oriented programming in C++. We will not begin with classes and objects right away. Instead we will focus on using multiple files first.

Other parts can be found here:
Part 2 – Class and object

1. First C++ program
To follow this tutorial you have to install C++ first. There are several ways. Here, I will assume that you have Codelite installed. So, run Codelite and do the following:

1. Create a new workspace
– Click on “Workspace” -> “New Workspace”. Type in “Tutorial” as Workspace name and confirm by clicking on the “Create” button.
– On the left hand side of Codelite there is a “Workspace” tab. Click on it. You can see the orange colored folder “Tutorial”.

2. Create a new project
– Rightclick on that orange “Tutorial” folder and click on “Create New Project”.
– Type in “helloWorld” as project name and click “OK”.
– On the left hand side, i.e. in the Workspace tab, you should see a folder “helloWorld”.
– Set the project as active: Either double click on the “helloWorld” folder or rightclick on it and choose “Set As Active”.
The active project will appear as green folder.

3. Code in main.cpp
– Left of the green “helloWorld” folder is a triangle. Click on it such that the yellow “src” folder appears.
– Click on the triangle left of the yellow “src” folder such that that you can see the “main.cpp” file.
– Double click on the main.cpp file. That file will already contain some code. Delete it and replace it with the following code:

#include <iostream>
using namespace std;

int main(){
	cout << "Hello World!" << endl;

	return 0;
}

4. Compile and run
– Compile the program by clicking Read the rest of this entry »

Posted in computer science | Tagged: , , , | Leave a Comment »

Tutorial: Object Oriented Programming in Python – Part 10

Posted by Ed on December 26, 2011

This is the continuation of the Python OOP tutorial. Today I want to describe how you can use Python in Eclipse by installing PyDev. Here, I will assume you have Eclipse installed and some familiarity with it.

Eclipse is a wonderful IDE known to Java users. I like it because of its auto-completion feature, e.g. if you use the dot operator on Read the rest of this entry »

Posted in computer science | Tagged: , , , , | Leave a Comment »

Tutorial: Object Oriented Programming in Python – Part 9

Posted by Ed on December 25, 2011

This is the continuation of the Python OOP tutorial. Today I want to talk about private variables and methods.

Private variables
Consider the class below. It describes Read the rest of this entry »

Posted in computer science | Tagged: , , , | Leave a Comment »

Tutorial: Object Oriented Programming in Python – Part 8

Posted by Ed on December 11, 2011

Update (03 Jan 2012):
When I wrote this tutorial I erroneously assumed that call by value and call by reference also exist in Python. However, I learned today that this is not the case. In Python you have something called call by object reference meaning:
– The value of a reference is copied. Keep in mind that the value of a reference is an address (see further reading down below).
– Everything in Python is an object, even integers. For example, b=1 means an object with value 1 is created (right hand side) and b (left hand side) is a reference to that object. Try this in the Python-Shell:

>>> b = 1
>>> id(b)
505497992
>>> b = 3
>>> id(b)
505498024

With id() you can check the address of b. You will notice that b has another address after the “assignment” b=3.

See my post: Call by Object Reference (Call by Sharing)

The section below was written before I knew about call by object reference.
—————————————————
 

This is the continuation of the Python OOP tutorial. Here I want to talk about the difference between call by value and call by reference. Besides, I’ve already explained something similar using C++ here.

Example 1 – Call by value:
Consider this code Read the rest of this entry »

Posted in computer science | Tagged: , , , , , , , | Leave a Comment »

Tutorial: Object Oriented Programming in Python – Part 6

Posted by Ed on December 10, 2011

This is the continuation of the Python OOP tutorial. Here, I will talk about modules in Python.

Before I address modules I want to Read the rest of this entry »

Posted in computer science | Tagged: , , , , , , , , , , , , , | Leave a Comment »

Tutorial: Object Oriented Programming in Python – Part 5

Posted by Ed on December 10, 2011

This is the continuation of the Python OOP tutorial. Here, I will talk about inheritance. The general syntax is:

class DerivedClass(BaseClass)

Example:
Last time (in part 4) we wrote a Vehicle class that allowed us to create objects with Read the rest of this entry »

Posted in computer science | Tagged: , , , , , , , , , , , | 3 Comments »

Tutorial: Object Oriented Programming in Python – Part 4

Posted by Ed on December 9, 2011

This is the continuation of the Python OOP tutorial. Today I describe how to change an attribute in a simple Vehicle class:

class Vehicle:

    def __init__(self, speed):
        self.speed = speed
        print("You have just created a vehicle.")

    def accelerate(self,x):
        self.speed = self.speed + x

    def brake(self,x):
        self.speed = self.speed - x

    def status(self):
        print("The speed of the vehicle is", self.speed, end=" km/h.")

Save the code in a file vehicle.py and Read the rest of this entry »

Posted in computer science | Tagged: , , , , , , , , | Leave a Comment »

Tutorial: Object Oriented Programming in Python – Part 3

Posted by Ed on December 7, 2011

This is part 3 of the tutorial on OOP in Python. The last time we talked about the __init__() method. We want to have a closer look at it:

class Fruit:
    # method
    def __init__(self, name, color, flavor):
        # set values for attributes
        self.name = name
        self.color = color
        self.flavor = flavor
        print("The", self.name, "is", self.color, "and tastes", self.flavor, end=".")

There are two things to notice:

1. The Fruit class has Read the rest of this entry »

Posted in computer science | Tagged: , , , , , , , , | Leave a Comment »