Object Oriented Software Design II: C++ Intro
Object Oriented Software Design II: C++ Intro
C++ intro
Giuseppe Lipari
March 1, 2013
1 Pointers
2 References
3 Copy constructor
4 Static
5 Constants
1 Pointers
2 References
3 Copy constructor
4 Static
5 Constants
class A { ... };
Pointer p contains the address of
A myobj; myobj
A *p = &myobj;
class A { ... };
Pointer p contains the address of
A myobj; myobj
A *p = &myobj;
class A { ... };
Pointer p contains the address of
A myobj; myobj
A *p = &myobj;
class A { ... };
Pointer p contains the address of
A myobj; myobj
A *p = &myobj;
class MyClass {
int a;
public:
MyClass(int i) { a = i; }
void fun(int y) { a = y; }
int get() { return a; }
};
void g(MyClass c) {
c.fun(5);
}
int main() {
MyClass obj(0);
cout << "Before calling g: obj.get() = " << obj.get() << endl;
g(obj);
cout << "After calling g: obj.get() = " << obj.get() << endl;
h(&obj);
cout << "After calling h: obj.get() = " << obj.get() << endl;
}
void f(){...}
MYFUNC funcPtr = f;
PTHREADFUN pt = mythread;
void f1(int a) {}
void f2(int a) {}
void f3(int a) {}
...
void (*funcTable []) (int) = {f1, f2, f3};
...
for (int i =0; i<3; ++i) (*funcTable[i])(i + 5);
class Data {
public:
int x;
int y;
};
1 Pointers
2 References
3 Copy constructor
4 Static
5 Constants
int x; // variable
int &rx = x; // reference to variable
class MyClass {
int a;
public:
MyClass(int i) { a = i; }
void fun(int y) { a = y; }
int get() { return a; }
};
void g(MyClass c) {
c.fun(5);
}
int main() {
MyClass obj(0);
cout << "Before calling g: obj.get() = " << obj.get() << endl;
g(obj);
cout << "After calling g: obj.get() = " << obj.get() << endl;
h(obj);
cout << "After calling h: obj.get() = " << obj.get() << endl;
}
1 Pointers
2 References
3 Copy constructor
4 Static
5 Constants
If the user does not define it, the compiler will define a default one
for us automatically
The default copy constructor just performs a bitwise copy of all
members
Remember: this is not a deep copy!
// can’t be copied!
class MyClass {
MyClass(const MyClass &r);
public:
...
};
1 Pointers
2 References
3 Copy constructor
4 Static
5 Constants
int ManyObj::count = 0;
int main()
{
ManyObj a, b, c, d;
ManyObj *p = new ManyObj;
ManyObj *p2 = 0;
cout << "Index of p: " << p->getIndex() << "\n";
{
ManyObj a, b, c, d;
p2 = new ManyObj;
cout << "Number of objs: " << ManyObj::howMany() << "\n";
}
cout << "Number of objs: " << ManyObj::howMany() << "\n";
delete p2; delete p;
cout << "Number of objs: " << ManyObj::howMany() << "\n";
}
Index of p: 4
Number of objs: 10
Number of objs: 6
Number of objs: 4
There is only one copy of the static variable for all the objects
All the objects refer to this variable
How to initialize a static member?
cannot be initialized in the class declaration
the compiler does not allocate space for the static member until it is
initiliazed
So, the programmer of the class must define and initialize the static
variable
int ManyObj::count = 0;
void func(ManyObj a)
{
...
}
void main()
{
ManyObj a;
func(a);
cout << "How many: " << ManyObj::howMany() << "\n";
}
void func(ManyObj a)
{
...
}
void main()
{
ManyObj a;
func(a);
cout << "How many: " << ManyObj::howMany() << "\n";
}
1 Pointers
2 References
3 Copy constructor
4 Static
5 Constants
You can use const for variables that never change after
initialization. However, their initial value is decided at run-time
int a
the pointer is constant
int * const u = &a;
class A {
public:
int i;
};
You can do the same thing with a pointer to a constant, but the
syntax is messy.
Remember:
we can pass argument by value, by pointer or by reference
in the last two cases we can declare the pointer or the reference to
refer to a constant object: it means the function cannot change it
Passing by constant reference is equivalent, from the user point of
view, to passing by value
From an implementation point of view, passing by const reference is
much faster!!
class A {
int i;
public:
int f() const;
void g();
};
void A::f() const
{
i++; // ERROR! this function cannot
// modify the object
return i; // Ok
}
const A a = ...;
a.f(); // Ok
a.g(); // ERROR!!