Beruflich Dokumente
Kultur Dokumente
Programming (OOP)
Introduction to C++
Overview
Additional features compared to C:
Small changes:
int a = 5;
int& b = a;
b = 10;
// a
int& foo(int& x)
{ x = 2; return
int y = 1;
foo(y);
foo(y) = 3;
= 10
x; }
// y = 2
// y = 3
Namespace
namespace ABC {
int x;
int setX(int y)
}
{ x = y; }
ABC::setX(20);
int z = ABC::x;
using namespace ABC;
setX(40);
4
delete a;
delete[] c;
{...}
{...}
{...}
{...}
#include <iostream>
using namespace std;
void main() {
int n;
cout << "Enter n: ";
cin >> n;
cout << "n = " << n << endl;
}
Introduction
From real world
Objects are things, facts, entities, which are characterized by
properties and can realize certain operations. Ex:
Each student is an object characterized by: name, age,
department, class, year, and can realizes: study, doing
exercises, going to class, doing tests,
Each cellphone is an object characterized by: SIM number,
model, size, and can realizes: making call, texting, answering
to calls, denying calls,
Classes are description of properties and operations of each type
of objects
Simpler consideration: student are objects while the notion of
student is a class, similarly for cellphones and the notion of
cellphone
Introduction (cont.)
to programming:
Class is a data type extended from structure type (struct). Apart from
fields that correspond to object properties, methods which are similar to
functions are added to realize operations
Object is a variable declared with type of the defined class
struct Student {
char name[20];
int year;
};
void go_to_class(Student& s, int room)
void do_test(Student& s)
{ ... }
{ ... }
Student s = { ... };
go_to_class(s, 103);
do_test(s);
9
Introduction (cont.)
to object-oriented programming:
struct Student {
char name[20];
int year;
void go_to_class(int room) { ... }
void do_test()
{ ... }
};
Student s = { ... };
s.go_to_class(103);
s.do_test();
10
Scope of members
Hung Long"); // OK
// OK
// error
// error
EE3490E: Programming T1 2015/2016
Dr. o Trung Kin Hanoi Univ. of Science and Technology
struct A {
int a; // public
};
class B {
int b; // private
};
Class example
#include <iostream>
class Circle {
private:
double r;
public:
void setR(double rr)
{ r = rr; }
double area()
{ return 3.14*r*r; }
double perimeter()
{ return 3.14*2.*r; }
};
void main() {
Circle c;
c.setR(1.23);
std::cout << "Area: " << c.area() << std::endl
<< "Perimeter: " << c.perimeter() << std::endl;
}
13
Constructors
Are initialization methods for objects. They do not return values, and invoked
automatically every times an object is created.
Possible to define many with different parameters, compiler knows which one
to call based on given parameters. If none defined, a default one is generated.
class Circle {
public:
Circle() { r = 0.; } // default constructor with no parameter)
Circle(double rr) { r = rr; }
...
};
Circle c1, c2(), c3[3];
Circle c4(1.23);
c1 = Circle;
c1 = Circle();
c1 = Circle(2.33);
Circle*
Circle*
Circle*
Circle*
14
c5
c6
c7
c8
=
=
=
=
new
new
new
new
//
//
//
//
//
constructor
constructor
error
constructor
constructor
Circle;
Circle();
Circle(3.22);
Circle[3];
//
//
//
//
1
2
1
2
constructor
constructor
constructor
constructor
1
1
2
1
Copy constructors
Used in creation of new objects from existing of the same class. Two
syntaxes for invoking copy constructors:
class Circle {
...
Circle(const Circle& c) { r = c.r; }
};
Circle c1(2.5);
Circle c2 = c1, c3(c1);
c2 = c1;
Cast constructors
class Ellipse {
...
Ellipse(const Circle& c) { rx = ry = c.r; }
};
Ellipse e1(c1), e2 = c2; // use cast constructor: Circle -> Ellipse
e1 = c1; // create a temp. object by implicit cast: Circle -> Ellipse
// then invoke assignment: Ellipse -> Ellipse
Possible to add explicit keyword in declaration of cast constructor to avoid being used in
implicit casts
16
class Circle {
...
Circle(double rr) {r = rr; }
};
Initialization lists
class Person {
private:
string name;
int age;
public:
Person(const char* n): name(n) { ... }
Person(const char* n, int a): name(n), age(a) { ...}
// ...
};
Person(const char* n) {
name = n;
// ...
}
Person(const char* n, int a) {
name = n;
age = a;
// ...
}
Must be used for constant and reference member variables, and any member variable
of class that does not have default constructor
EE3490E: Programming T1 2015/2016
17
Destructors
18
class String {
private:
char* str;
public:
String()
{ str = NULL; }
String(const char* s)
{ str = new char[strlen(s)+1]; strcpy(str, s); }
~String()
{ if (str) delete str; }
...
};
String* abc() {
String s1, s2("Hello!"), s3[3];
String *s4 = new String("xyz"), *s5 = new String[3];
String *s6 = new String("abc");
delete s4;
// destroy 1 object
delete[] s5;
// destroy 3 objs, dont use: delete s5
return s6;
// destroy s1, s2, s3[3] but s6 is still there
EE3490E: Programming T1 2015/2016
}
string.cpp
class String {
private:
char* str;
public:
String();
String(const char* s);
~String();
void set(const char* s);
const char* get();
};
String::String()
{ str = NULL; }
String::String(const char* s)
{ str = NULL; set(s); }
String::~String()
{ if (str) delete str; }
void String::set(const char* s) {
if (str) delete str;
if (!s) { str = NULL; return; }
str = new char[strlen(s)+1];
strcpy(str, s);
}
const char* String::get()
{ return str; }
19
class Circle {
private:
double r;
public:
...
friend void printCircle(Circle c);
friend class Ellipse;
};
void printCircle(Circle c)
{ cout << "Radius: " << c.r; }
class Ellipse {
private:
double rx, ry;
public:
void convert(Circle c) {
rx = ry = c.r;
}
};
20
this pointer
class Buffer;
void do_smth(Buffer* buf);
class Buffer {
private:
char* b;
int n;
public:
Buffer(int n)
{ this->n = n; this->b = new char[n]; }
~Buffer()
{ delete this->b; }
void some_method()
{ do_smth(this); }
};
Buffer buf(4096);
buf.some_method();
Static member variables are ones that are unique for all objects of
same classes (even when no object exists)
Static methods are ones that make access only to static member
variables
class C {
public:
static int count;
C()
{ count++; }
~C()
{ count--; }
static int getCount()
{ return count;
}
...
};
int C::count = 0;
Its possible to access static members via class name and scope
operator ::, or via objects as normal members of that class
this pointer is not defined in static methods
22
Constant methods
Within a constant method, only constant member methods of that object are
accessible
One would rather declare all methods that does not change member variables as
constant
Circle c1;
class Circle {
const Circle c2(2.333);
...
void setR(double r) { this->r = r; }
double getR() const { return r; }
c1.setR(1.22); // OK
double area() const { return PI*r*r; }
c2.setR(1.22); // error
void cf(double r) const {
c1.area();
// OK
area();
// OK
c2.area();
// OK
setR(r);
// error
this->r = r; // error
cout << c1.getR() // OK
}
};
<< c2.getR(); // OK
23
Problems
1.
2.
3.
4.
5.
6.
24