Skip to content
Snippets Groups Projects
Commit 0139e46c authored by Sebastien Ponce's avatar Sebastien Ponce
Browse files

Merge branch 'add-some-oo-exercise' into 'master'

Add some oo exercise

See merge request sponce/cpluspluscourse!15
parents 91a3d645 4a6d1741
No related branches found
No related tags found
1 merge request!15Add some oo exercise
......@@ -20,6 +20,10 @@ poly->computePerimeter();
```
and check what is called, in regular and virtual method case
### modern_oo
Insert one `= delete`, one `= default` and one `override`.
### virtual_inheritance
First create a TextBox and try to call draw.
There will be an error that the member is ambiguous, due to multiple inheritance.
......
## Prerequisites
* know about classes in ancient C++
## Instructions
* insert one `= delete`, one `= default` and one `override`.
#include <cstdlib> // pour std::rand()
#include <iostream>
#include <string>
class Particle
{
public :
Particle( double mass ) : mass_(mass) {}
double mass() { return mass_ ; }
virtual std::string name() { return "Particle" ; }
virtual ~Particle() {}
private :
Particle( const Particle & ) ; // non copiable
double mass_ ;
} ;
class ChargedParticle : public Particle
{
public :
ChargedParticle( double mass, double charge )
: Particle(mass), charge_(charge) {}
double charge() { return charge_ ; }
virtual std::string name() { return "ChargedParticle" ; }
private :
double charge_ ;
} ;
void print( Particle & p )
{
std::cout << p.name() << std::endl ;
std::cout << " mass = " << p.mass() << std::endl ;
}
int main()
{
for ( int i = 0 ; i < 5 ; ++i )
{
if ( std::rand() < (0.5 * double(RAND_MAX)) )
{
Particle p(2) ;
print(p) ;
}
else
{
ChargedParticle p(1,1) ;
print(p) ;
std::cout << " charge = " << p.charge() << std::endl ;
}
}
}
\ No newline at end of file
#include <cstdlib> // pour std::rand()
#include <iostream>
#include <string>
class Particle
{
public :
Particle( double mass ) : mass_(mass) {}
Particle( const Particle & ) = delete ;
virtual ~Particle() = default ;
double mass() { return mass_ ; }
virtual std::string name() { return "Particle" ; }
private :
double mass_ ;
} ;
class ChargedParticle : public Particle
{
public :
ChargedParticle( double mass, double charge )
: Particle(mass), charge_(charge) {}
double charge() { return charge_ ; }
std::string name() override { return "ChargedParticle" ; }
private :
double charge_ = 0.0 ;
} ;
void display( Particle & p )
{
std::cout << p.name() << std::endl ;
std::cout << " mass = " << p.mass() << std::endl ;
}
int main()
{
for ( int i = 0 ; i < 5 ; ++i )
{
if ( std::rand() < (0.5 * double(RAND_MAX)) )
{
Particle p(2) ;
display(p) ;
}
else
{
ChargedParticle p(1,1) ;
display(p) ;
std::cout << " charge = " << p.charge() << std::endl ;
}
}
}
\ No newline at end of file
## Instructions for the "polymorphism" exercise
Step 1
* look at the code
* open `trypoly.cpp`
* create a Pentagon, call its `perimeter` method
......@@ -10,6 +11,8 @@
```
* create an Hexagon, call its `perimeter` method
* recompile and check what happens
Step 2
* create an Hexagon, call its parent’s `perimeter` method
* recompile and check again
* retry with virtual methods
......
## Instructions
Step 1
* look at the code
* open `test.cpp`
* create a `TextBox` and call `draw`
* Fix the code to call both draws by using types
Step 2
* retry with virtual inheritance
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment