Object Oriented Programming
A sample class
#include <iostream>
class Fraction
{
// Available only for class members
private:
# Data members/attributes
int nominator;
int denominator;
// Available for any element of a program
public:
// Constructor - Function member (or method)
// Does not have a return type
// Runs at the creation of a class member
Fraction (int n = 0, int d = 1);
// Mutator
void setValue (int n, int d = 1);
// Accessor
int getNominator();
int getDenominator();
void print();
}
The private class statement is not needed, any data member should be (by convention) a private data member.
Note, that the class does not have function methods inside of it.
Fraction(int n = 0, d = 1);
Despite being a function, Fraction has a given parameters. Those are the values that are given as default in a situation where a user does not give the parameters.
We can also have:
// setValue needs at least one given parameter
void setValue(int n, int d = 1);
If a parameter has a static value, all next parameters must have default values. We cannot do something like this:
void setValue(int n = 1, int d);
Results:
int main()
{
Fraction a;
// a = 0/1
Fraction b(2);
// b = 2/1
Fraction c(3,4)
// c = 3/4
}
Function definitions outside the class
/*
* The default parameter values can be set only once
* You can set them in the class definition,
* or in the function methods.
* They cannot be given twice
*/
Fraction::Fraction(int n, int d) {
nominator = n;
denominator = d;
// This will allow division by 0 !!! result -> infinity
}
void Fraction::setValue(int n, int d) {
nominator n;
denominator = d;
// This will allow division by 0 !!! result -> infinity
}
int Fraction::getNominator() {
return nominator;
}
Namespaces
Every class creates its own namespace. That's why we use Fraction::XYZ
We should NOT set using namespace std at the beginning of the code, as it may interfere with the class namespaces.
Repeating functions
We can repeat a function definition with given default arguments, as long as the default arguments don't repeat.
// Valid normal definition
Fraction f(int x, int y, int z = 3);
// Valid, adding y=2 as a default argument
Fraction f(int x, int y = 2, int z);
// Valid, adding x=1 as a default argument
Fraction f(int x = 1, int y, int z);
// Error, redefinition of default arguments
Fraction f(int x = 1, int y = 2, int z = 3);
Function signature
Every function consists of:
- Function name
- List of parameter types
Those values create a function signature by which the compiler chooses correct function to run when a call occurs.
This allows us to do this:
void print(int i);
void print(double d);
int a = 3;
# Will use the first print function
print(a);
# Will use the second print function
print(5.3);
Friend
friend Fraction operator(const Fraction& x, const Fraction& y);
VERY DANGEROUS
The friend keyword allows the object to have the access to private data of another object.
Destructor
Destructor should be only a private, runtime function.
class Test
{
public:
// Constructor - run on the object creation
Test() { cout << "Object is created"; }
//Destructor - run on the object destruction
~Test() { cout << "Object was destroyed"; }
}
The destructor is run when:
- The program ends
- The function ends
- Block containing local variables ends
deleteoperator is called
Assignment operator
class FractionSet()
{
private:
// as previously
public:
FractionSet();
~FractionSet();
void insertElement(const Fraction& f);
void insertE
// something something
}
FractionSet& FractionSet::operator=(const FractionSet& x)
{
// Self assignment
if (this == &x)
return *this;
element* etr = head;
while (etr)
{
etr = etr -> next;
delete head;
head = etr;
}
element *xtr = x.head;
while (xtr)
{
Fraction copied = xtr -> data;
element* ntr = new element;
if (!ntr)
{
cerr << "FractionSet::operator=: Allocation memory failure!\n";
break;
}
ntr -> data = copied;
ntr -> next = NULL;
if (!head)
head = ntr;
else
etr -> next = ntr;
etr = ntr;
xtr = xtr -> next;
number = x.number;
return *this
}
}
University/WUT/EOOP/Preliminary Project - Basics
University/WUT/EOOP/Project - Taxi Agency
University/WUT/EOOP/Midterm test 1
University/WUT/EOOP/Classes II