Andrés Cabrera
(+ 3 4)
int a = 3;
int b = 4;
int c = a + b;
Numbers n;
n.setNumbers(3,4);
c = n.add();
Functional
Procedural
Object oriented
class Number
{
public:
void setNumbers(int c, int d) {
a = c;
b = d;
}
int add() {
return a + b;
}
private:
int a;
int b;
}
class Number:
self.a = 0
self.b = 0
def setNumbers(self, c,d):
self.a = c
self.b = d
def add(self):
return a + b
}
Number n;
Number m;
n.setNumbers(3.5,4);
m.setNumbers(5,6);
n.add(); // C returns 7, Python return 7.5
m.add(); // Return 11
Number n;
n.setNumbers(3,4);
int c = n.add();
printf("%i", n.a); // Error a is a private member
Members of an object are accessed using '.'
whether they are methods or attributes
n = Number();
n.setNumbers(3,4);
c = n.add();
print n.a // Not private!!
Everything is an object in Python!
class Numbers
{
public:
Numbers(int a = 0, int b = 0) {
c = a;
d = b;
}
~Numbers() {
// The destructor
}
int add() {
return c + d;
}
private:
int c;
int d;
}
int main(int argc, char *argv[])
{
Numbers n(3,4);
int c = n.add();
}
class Shape:
def __init__(self, length):
self.type = 'none'
def area(self):
return 0
def getType(self):
return self.type
class Square(Shape):
def __init__(self, length):
self.type = 'square'
self.length = length
def area(self):
return self.length**2
class Circle(Shape):
def __init__(self, radius):
self.type = 'circle'
self.radius = radius
def area(self):
return 3.14159 * self.radius**2
c = Circle(2)
print c.area() # prints 12.56636 (4 * pi)
c = Square(2)
print c.area() # prints 4
There's a bit more to it:
class ClassName(BaseClass):
class ClassName:
def __init__(self, arg1, arg2):
self.AttributeName
def method(self, arg1, arg2 ...):
self.method(arg1, arg2)
class Shape:
def __init__(self, length):
self.type = 'none'
def area(self):
return 0
def getType(self):
return self.type
class Square(Shape):
def __init__(self, length):
self.type = 'square'
self.length = length
def area(self):
return self.length**2
class Circle(Shape):
def __init__(self, radius):
self.type = 'circle'
self.radius = radius
def area(self):
return 3.14159 * self.radius**2
c = Circle(2)
print c.area() # prints 12.56636 (4 * pi)
c = Square(2)
print c.area() # prints 4
class ClassName {}
ClassName(int arg1, ...) {}
int ClassName::function(int arg1, ...) {}
class Numbers
{
public:
Numbers(int a = 0, int b = 0) {
c = a;
d = b;
}
~Numbers() {
// The destructor
}
int add() {
return c + d;
}
private:
int c;
int d;
}
int main(int argc, char *argv[])
{
Numbers n(3,4);
int c = n.add();
}
namespace::function()
using namespace
template <typename Type>
Type max(Type a, Type b) {
return a > b ? a : b;
}
#include <iostream>
int main()
{
// This will call max<int> by implicit argument deduction.
std::cout << max(3, 7) << std::endl;
// This will call max<double> by implicit argument deduction.
std::cout << max(3.0, 7.0) << std::endl;
// This depends on the compiler. Some compilers handle this by defining a template
// function like double max <double> ( double a, double b);, while in some compilers
// we need to explicitly cast it, like std::cout << max<double>(3,7.0);
std::cout << max(3, 7.0) << std::endl;
std::cout << max<double>(3, 7.0) << std::endl;
return 0;
}
From: https://en.wikipedia.org/wiki/Template_%28C%2B%2B%29
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello World" << endl;
return 0;
}
(But that are important and you will find mentioned often)
e.g. Twitter web API
#include <vector>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> v;
string word1 = "Hello";
string space = " ";
string word2 = "World";
string out = word1 + space + word2;
v.push_back(word1.length());
v.push_back(word2.length());
return 0;
}