Skip to content

Commit 6090fc7

Browse files
committed
Rename files and change CMakelists
1 parent 6f18f3b commit 6090fc7

File tree

6 files changed

+62
-65
lines changed

6 files changed

+62
-65
lines changed

Polymorphism/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

88
# Adding multiple executable to the project
99
# Right-click on each item to set as start-up project
10-
add_executable(AnimalExample
11-
"example_Animal.hpp"
12-
"example_Animal.cpp"
10+
add_executable(animal
11+
"animal.hpp"
12+
"animal.cpp"
1313
)
14-
add_executable(PolygonExample "example_Polygon.cpp")
15-
add_executable(ClassPetLab "lab_Pet.cpp")
14+
add_executable(polygon "polygon.cpp")
15+
add_executable(pet "pet.cpp")

Polymorphism/example_Animal.cpp renamed to Polymorphism/animal.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
* Created on: Nov 06, 2020
1010
*/
1111

12+
#include "animal.hpp"
1213
#include <iostream>
1314
#include <string>
14-
#include "example_Animal.hpp"
1515

1616
int main(void)
1717
{
1818
Animal myAnimal;
19-
Pig myPig;
20-
Dog myDog;
19+
Pig myPig;
20+
Dog myDog;
2121

2222
std::cout << "Sound from class \"Animal\":";
2323
myAnimal.animalSound();

Polymorphism/animal.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#include <iostream>
3+
4+
// Base class
5+
class Animal
6+
{
7+
public:
8+
void animalSound() { std::cout << "The animal makes a sound." << "\n"; }
9+
};
10+
11+
// Derived class
12+
class Pig : public Animal // inheritting from base class 'Animal'
13+
{
14+
public:
15+
void animalSound() { std::cout << "The pig says: wee wee." << "\n"; }
16+
};
17+
18+
// Derived class
19+
class Dog : public Animal // inheritting from base class 'Animal'
20+
{
21+
public:
22+
void animalSound() { std::cout << "The dog says: bow wow." << "\n"; }
23+
};

Polymorphism/example_Animal.hpp

Lines changed: 0 additions & 33 deletions
This file was deleted.

Polymorphism/lab_Pet.cpp renamed to Polymorphism/pet.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// clang-format off
12
/*****************************************************************//**
23
* \file lab_Polymorphism.cpp
34
* \brief lab on polymorphism with keyword "virtual"
@@ -9,6 +10,7 @@
910
* \author Xuhua
1011
* \date November 2020
1112
*********************************************************************/
13+
// clang-format on
1214

1315
#include <iostream>
1416
#include <string>
@@ -17,18 +19,19 @@ class Pet
1719
{
1820
protected: // X.H. changed the keyword to 'protected'
1921
std::string name;
20-
int age;
22+
int age;
2123

2224
public:
2325
// default and overloaded constructors
24-
Pet() {
26+
Pet()
27+
{
2528
setAge(0);
2629
setName("");
2730
}
2831

2932
// set and get function:
30-
void setName(std::string newname) { name = newname; }
31-
std::string getName() const{ return name; }
33+
void setName(std::string newname) { name = newname; }
34+
std::string getName() const { return name; }
3235

3336
int getAge() const { return age; }
3437

@@ -44,12 +47,13 @@ class Dog : public Pet // inheriting from class 'Pet'
4447

4548
public:
4649
// default and overloaded constructor
47-
Dog() {
50+
Dog()
51+
{
4852
setAge(getAge() * 7); // access to class Pet is granted
4953
}
5054

5155
// set and get function for new element std::string breed:
52-
void setBreed(std::string newbreed) { breed = newbreed; }
56+
void setBreed(std::string newbreed) { breed = newbreed; }
5357
std::string getBreed() const { return breed; }
5458

5559
// new setAge function
@@ -63,29 +67,27 @@ class Cat : public Pet // inheriting from class 'Pet'
6367

6468
public:
6569
// default constructor
66-
Cat() {
67-
setnumLives(9);
68-
}
70+
Cat() { setnumLives(9); }
6971

7072
// set and get functions for new element int numLives:
7173
void setnumLives(int newNumLives) { numLives = newNumLives; }
72-
int getnumLives(void) const { return numLives; }
74+
int getnumLives(void) const { return numLives; }
7375

7476
// new setAge function
7577
void setAge(int newage) override { age = newage; }
7678
};
7779

7880
int main(void)
7981
{
80-
Pet myPet; // created a 'Pet' object
82+
Pet myPet; // created a 'Pet' object
8183
Pet* ptrPet = &myPet;
8284
// create a pointer of class 'Pet'
8385
// and linked it to Pet 'myPet'
8486

85-
Dog myDog;
87+
Dog myDog;
8688
Pet* ptrDog = &myDog; // parent class pointer pointing to derived class object
8789

88-
Cat myCat;
90+
Cat myCat;
8991
Pet* ptrCat = &myCat; // parent class pointer pointing to derived class object
9092

9193
// with three pointers, setting all ages to 2
@@ -95,13 +97,13 @@ int main(void)
9597
ptrCat->setAge(2);
9698

9799
std::cout << "\nTest case with virtual keyword: " << "\n"
98-
<< "Obtaining age for Pet object which should be 0: " << ptrPet->getAge() << "\n"
99-
<< "Obtaining age for Dog object which should be 14: " << ptrDog->getAge() << "\n"
100-
<< "Obtaining age for Cat object which should be 2: " << ptrCat->getAge() << "\n";
100+
<< "Obtaining age for Pet object which should be 0: " << ptrPet->getAge() << "\n"
101+
<< "Obtaining age for Dog object which should be 14: " << ptrDog->getAge() << "\n"
102+
<< "Obtaining age for Cat object which should be 2: " << ptrCat->getAge() << "\n";
101103

102104
std::cout << "\nComment out the \"virtual\" keyword to verify all ages are 0." << "\n"
103-
<< "\tSince all pointers are forced to go through" << "\n"
104-
<< "\tmember function setAge() in \"Pet\" class." << "\n";
105+
<< "\tSince all pointers are forced to go through" << "\n"
106+
<< "\tmember function setAge() in \"Pet\" class." << "\n";
105107

106108
return 0;
107109
}

Polymorphism/example_Polygon.cpp renamed to Polymorphism/polygon.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
// clang-format off
12
/*****************************************************************//**
2-
* \file example_Polygon.cpp
3+
* \file polygon.cpp
34
* \brief
45
*
56
* 1) The virtual keyword allows a member function of a derived class
@@ -12,6 +13,7 @@
1213
* \author Xuhua
1314
* \date November 2020
1415
*********************************************************************/
16+
// clang-format on
1517

1618
#include <iostream>
1719

@@ -21,9 +23,12 @@ class Polygon
2123
int width, height;
2224

2325
public:
24-
2526
// function prototype
26-
void set_values(int a, int b) { width = a; height = b; }
27+
void set_values(int a, int b)
28+
{
29+
width = a;
30+
height = b;
31+
}
2732

2833
// "Polygon" is a 'polymorphic class'
2934
virtual int area() const { return 0; } // virtual keyword ensures
@@ -45,9 +50,9 @@ class Triangle : public Polygon
4550
int main()
4651
{
4752
Rectangle rect;
48-
Triangle trgl;
49-
Polygon poly;
50-
Polygon* ppoly;
53+
Triangle trgl;
54+
Polygon poly;
55+
Polygon* ppoly;
5156

5257
ppoly = &rect; // point to a "Rectangle" class object
5358
ppoly->set_values(4, 5);

0 commit comments

Comments
 (0)