Lecture 1 "Effective Programm Technique"/ Politechnika Wroclawska
Lecture 1 "Effective Programm Technique"/ Politechnika Wroclawska
Lecture 1 "Effective Programm Technique"/ Politechnika Wroclawska
Communication
➢ For questions, email to [email protected]
with 'Subject=your name”.
➢ Make sure to email from an account I can reply to.
➢ All course information will be available at:
http://www.ii.pwr.wroc.pl/~kwiatkow
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
Content
➢ The aim of the course is to present the essential properties of
object-oriented programming languages basing at the C++
programming language.
➢ The course will focus on presenting techniques that guarantee
developing and implementing efficient programs.
➢ The following topics are covered:
objects and classes, encapsulation and information hiding,
constructors and destructors, friend functions and classes,
operators overloading, inheritance, virtual methods and
polymorphism, exception handling, function and class
templates, containers and iterators.
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
Literature
➢ B. Stroustrup, The C++ Programming language, Addison-
Wesley Pub. 1993
➢ H.M. Deitel, P.J. Deitel, C++ How to program, Prentice Hall
2003
➢ B. Eckel, Thinking in C++, Pearson Education 2000.
➢ Scott Meyers „Effective modern C++”, O’Reilly Media, 2014.
➢ Documentation of the STL library
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
class Point
{
public:
Point();
int Get_x();
int Get_y();
private:
int x,y;
};
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
main()
{
Point p;
p.Get_x();
p.Get_y();
}
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
Friend functions
➢ Allow non-member functions access to private members of class
class myClass {
int a, b;
public:
myClass(int I, int j) {a=I; b=j;}
friend int comDenom(myClass x);
};
i nt comDenom(myClass x)
{ int max = x.a < x.b ? x.a : x.b;
for (int i=2; i <= max; i++)
if ((x.a%i) == 0 && (x.b%i) == 0) return i;
return 0;
}
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
Constructor
➢ Constructors are invoked to initialize the data members of a class.
➢ Can not return any value (not even void).
➢ Can accept any parameters as needed.
➢ Constructors differs from other member functions.
➢ Initializes a newly created object, other member functions are
invoked by existing objects.
➢ A Constructor is invoked automatically when an object is created.
➢ Have the same name as the class.
➢ You can have several constructors for a class by overloading them.
➢ If you implement no constructor, the compiler automatically
generates a default constructor for you
➢ But if you write any constructors at all, the compiler does not supply
a default
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
Pointers
The pointer is used to indicate (show) objects of a given type, it cannot be used
to point to objects of a different type.
int d = 5; // an object of type int
int k = 3; // an object of type int
int * w; // pointer “w” is defined
w = & k; // setting the pointer to the object "k"
int * w = & k; // it is correct, too
Now you can change the pointed value in two ways:
k = 100; or * w = 100;
The pointer can be set to point to the object "d"
w = & d; // set the pointer to object "d"
But you can't do that
float c;
w = & c; //error ???
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
Pointers
➢ Let's define an example class
class test {
public:
int thing; }
➢ How can we relate to class (object) members?
test object1; // we define the object of the class "test"
test * pointer; // we define the pointer
test & object2 = object1; // we define the reference
object1.thing = 10; // by the name of the object
Pointer = & object1;
pointer -> thing = 10; // using the pointer
object2.thing = 10; // using the reference
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
This pointer
➢ “this” is a pointer to the object through which this method was
invoked
int getSize() { return size; }
int getSize() { return this->size; }
➢ Passed as an implicit argument to every member method
➢ Common uses for “this”:
▪ To call a method in another object, passing the current object as an
argument
▪ To return a pointer to the current object
➢ Friend functions don’t have a “this” pointer, because
➢ friends are not members of a class.
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
Data structures
➢ Dynamic data structures
▪ Data structures that grow and shrink during execution
➢ Linked lists
▪ Allow insertions and removals anywhere
➢ Stacks
▪ Allow insertions and removals only at top of stack
➢ Queues
▪ Allow insertions at the back and removals from the front
➢ Binary trees
▪ High-speed searching and sorting of data and efficient
elimination of duplicate data items
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
15 10
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
An example
Let’s define a class account
➢ It should be stored in two files: account.h – the definition of the class and
account.cpp – the definition of the functions
In account.h
class account {
protected:
int accountNumber;
char* owner;
float accountBalance;
public:
account (int theNumber, char* theOwner, float theBalance);
virtual void display();
virtual void makeLodgement(float lodgement);
virtual void makeWithdrawal (float withfrawal);
~account(); };
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”
An example
In account.cpp
#include ”account.h”
account::account (int theNumber, char* theOwner, float
theBalance)
{ // code for account::account }
void account::display();
{ // code for account::display }
void account:: makeLodgement(float lodgement);
{ // code for account:: makeLodgement }
void account:: makeWithdrawal (float withfrawal);
{ // code for account:: makeWithdrawal }
account:: ~account()
{ // code for account:: ~account }
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej”