Skip to content

Commit 470c269

Browse files
Carlo Lanzi Lucianicarlolalu
Carlo Lanzi Luciani
authored andcommitted
Chapter 24 till x.2
1 parent 8ec7c8f commit 470c269

8 files changed

+237
-8
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# my_learncpp
2-
'learncpp' + random exercises
2+
'learncpp.com' + random exercises
33

44
Here I add various exercises and eventually small projects to learn cpp, following mainly the website learncpp.com. Others are taken from the project euler (for which a high level language like python or julia would be better) or eventually edabit or similar.
55

@@ -8,5 +8,6 @@ PS: all these exercises are compiled with this command:
88
# compiler as learncpp adivises
99
# with the right standards in debug conf
1010
alias gpp-pedantic='g++ -ggdb -pedantic-errors -Wall -Weffc++ -Wextra -Werror -Wsign-conversion -std=c++20'
11+
```
1112

12-
```
13+
- update: the website `learncpp.com` is sometimes updated, and as can be verified in the middle of my tutorial it got updated. Thus the two folders. Because of this reason it becomes VERY important to havea reference book, a pdf or similar, to which I can get acquainted to better retrieve information from it, rather than having this website, which is excellent to learn but surely not as a reference.

learncpp.com-v09.2023/bookmark.txt

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
topic 1: learncpp.com (one day I will be able to write here 'FINISHED!')
2-
24.2
2+
Bookmark: 24.x
33

4-
topic 2: learn smt about computer networks from the book 'a top down approach'.
5-
Bookmark : ch2 - 2.1.3 (remember homeworks when you finish 2.1 and wireshark lab)
4+
at the end of ch26, return to check the lesson 24.9 on the navtab 'for advanced readers'
65

7-
left behind: most of problems of ch1 (beautiful mathematical things)
86

9-
From now on I advise to do a section and then some problems every time. Another option is to use the book of Kurose ONLINE. It has a lot of good exercises and powerpoint presentations!!! I have also to select just a number of exercises, cannot do all of them
7+
8+
topic 2: learn smt about computer networks from the book 'a top down approach' + exercises
9+
Bookmark (ch.section(.subsection)): 2.2.1 (remember to do the exercises at the end of each section)
1010

1111
topic 2b: practice of topic2 following the website of the book
12+
Bookmark: HTTP of the wireshark lab
13+
14+
topic 2c: interactive quiz from the website of Kurose
15+
16+
future topic 3: rust somehow
17+
18+
----------------------------- already done -------------------------------
19+
20+
practiced from topic2b: introduction
21+
22+
Exercises form the book already done (at least speaking out loud):
23+
2.1
24+
25+
Ex still to be done: all of ch1 (beautiful math ones)
26+
27+

learncpp.com-v09.2023/ch24/4.1.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Let’s implement our Fruit example that we talked about in our introduction to inheritance. Create a Fruit base class that contains two private members: a name (std::string), and a color (std::string). Create an Apple class that inherits Fruit. Apple should have an additional private member: fiber (double). Create a Banana class that also inherits Fruit. Banana has no additional members.
3+
4+
The following program should run:
5+
6+
#include <iostream>
7+
8+
int main()
9+
{
10+
const Apple a{ "Red delicious", "red", 4.2 };
11+
std::cout << a << '\n';
12+
13+
const Banana b{ "Cavendish", "yellow" };
14+
std::cout << b << '\n';
15+
16+
return 0;
17+
}
18+
19+
And print the following:
20+
21+
Apple(Red delicious, red, 4.2)
22+
Banana(Cavendish, yellow)
23+
24+
Hint: Because a and b are const, you’ll need to mind your consts. Make sure your parameters and functions are appropriately const.
25+
*/
26+
#include <string>
27+
#include <iostream>
28+
29+
class Fruit
30+
{
31+
private:
32+
std::string m_name {};
33+
std::string m_color {};
34+
35+
public:
36+
Fruit(std::string name, std::string color) : m_name {name}, m_color {color}
37+
{}
38+
39+
std::string_view getName() const
40+
{
41+
return m_name;
42+
}
43+
44+
std::string_view getColor() const
45+
{
46+
return m_color;
47+
}
48+
};
49+
50+
class Banana : public Fruit
51+
{
52+
private:
53+
public:
54+
Banana(std::string name, std::string color="yellow") : Fruit{name, color}
55+
{}
56+
57+
friend std::ostream& operator << (std::ostream& out, const Banana& banana)
58+
{
59+
out << "(" << banana.getName() << ", " << banana.getColor() << ")";
60+
return out;
61+
}
62+
};
63+
64+
class Apple : public Fruit
65+
{
66+
private:
67+
double m_fiber {};
68+
69+
public:
70+
Apple(std::string name, std::string color, double fiber) : Fruit{name, color} , m_fiber {fiber}
71+
{}
72+
73+
double getFiber() const
74+
{
75+
return m_fiber;
76+
}
77+
78+
friend std::ostream& operator << (std::ostream& out, const Apple& apple)
79+
{
80+
out << "(" << apple.getName() << ", " << apple.getColor() << ", " << apple.getFiber() << ")";
81+
return out;
82+
}
83+
};
84+
85+
86+
87+
int main()
88+
{
89+
const Apple a{ "Red delicious", "red", 4.2 };
90+
std::cout << a << '\n';
91+
92+
const Banana b{ "Cavendish", "yellow" };
93+
std::cout << b << '\n';
94+
95+
return 0;
96+
}

learncpp.com-v09.2023/ch24/x.2.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* a) Write an Apple class and a Banana class that are derived from a common Fruit class. Fruit should have two members: a name and a color.
3+
4+
The following program should run:
5+
6+
int main()
7+
{
8+
Apple a{ "red" };
9+
Banana b{};
10+
11+
std::cout << "My " << a.getName() << " is " << a.getColor() << ".\n";
12+
std::cout << "My " << b.getName() << " is " << b.getColor() << ".\n";
13+
14+
return 0;
15+
}
16+
17+
And produce the result:
18+
19+
My apple is red.
20+
My banana is yellow.
21+
22+
23+
24+
b) Add a new class to the previous program called GrannySmith that inherits from Apple.
25+
26+
The following program should run:
27+
28+
int main()
29+
{
30+
Apple a{ "red" };
31+
Banana b;
32+
GrannySmith c;
33+
34+
std::cout << "My " << a.getName() << " is " << a.getColor() << ".\n";
35+
std::cout << "My " << b.getName() << " is " << b.getColor() << ".\n";
36+
std::cout << "My " << c.getName() << " is " << c.getColor() << ".\n";
37+
38+
return 0;
39+
}
40+
41+
And produce the result:
42+
43+
My apple is red.
44+
My banana is yellow.
45+
My granny smith apple is green.
46+
47+
*/
48+
#include <string>
49+
#include <string_view>
50+
#include <iostream>
51+
52+
class Fruit
53+
{
54+
private:
55+
std::string m_name {};
56+
std::string m_color {};
57+
58+
public:
59+
Fruit(std::string name, std::string color) : m_name {name}, m_color {color}
60+
{}
61+
62+
std::string_view getName() const
63+
{
64+
return m_name;
65+
}
66+
67+
std::string_view getColor() const
68+
{
69+
return m_color;
70+
}
71+
};
72+
73+
class Banana : public Fruit
74+
{
75+
private:
76+
public:
77+
Banana(std::string name="banana", std::string color="yellow") : Fruit{name, color}
78+
{}
79+
};
80+
81+
class Apple : public Fruit
82+
{
83+
private:
84+
protected:
85+
Apple(std::string name, std::string color) : Fruit(name, color)
86+
{}
87+
88+
public:
89+
Apple(std::string color="red") : Fruit{"apple", color}
90+
{}
91+
};
92+
93+
class GrannySmith : public Apple
94+
{
95+
private:
96+
public:
97+
GrannySmith() : Apple{"granny smith apple", "green"}
98+
{}
99+
};
100+
101+
102+
int main()
103+
{
104+
Apple a{ "red" };
105+
Banana b;
106+
GrannySmith c;
107+
108+
std::cout << "My " << a.getName() << " is " << a.getColor() << ".\n";
109+
std::cout << "My " << b.getName() << " is " << b.getColor() << ".\n";
110+
std::cout << "My " << c.getName() << " is " << c.getColor() << ".\n";
111+
112+
return 0;
113+
}

learncpp.com-v09.2023/ch24/x.3.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
*
3+
*/
File renamed without changes.

projecteuler.net/project_euler_problem2.cpp singlefiles/project_euler_problem2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int main(void)
1313
}
1414

1515

16-
//brute force system: will likely crash for the lack of memory
16+
//brute force system
1717
long long sum_even_fibonacci(long upperbound)
1818
{
1919
long tmp;

0 commit comments

Comments
 (0)