-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16-classes-objects.cpp
More file actions
148 lines (113 loc) · 3.35 KB
/
16-classes-objects.cpp
File metadata and controls
148 lines (113 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
using namespace std;
class Book {
public:
string title;
string author;
int pages;
Book() {
cout << "Creating object" << endl;
}
// Sobrecarga de constructores.
Book(string aTitle, string aAuthor, int aPages) {
title = aTitle;
author = aAuthor;
pages = aPages;
}
private:
string privateField;
};
class Student {
public:
string name;
string major;
double gpa;
Student(string aName, string aMajor, double aGpa) {
name = aName;
major = aMajor;
gpa = aGpa;
}
// Método de instancia público.
bool hasHonors() {
return gpa >= 3.5;
}
};
class Movie {
private:
string title;
string director;
string rating;
public:
Movie(string aTitle, string aDirector, string aRating) {
title = aTitle;
director = aDirector;
setRating(aRating);
}
void setRating(string aRating) {
if (aRating == "G" || aRating == "PG" || aRating == "PG-13" || aRating == "R" || aRating == "NR")
rating = aRating;
else
rating = "NR";
}
string getRating() {
return rating;
}
};
class Chef {
public:
void makeChicken() {
cout << "The chef makes chicken" << endl;
}
void makeSalad() {
cout << "The chef makes salad" << endl;
}
void makeSpecialDish() {
cout << "The chef makes bqq ribs" << endl;
}
};
// Herencia.
class ItalianChef: public Chef {
public:
void makePasta() {
cout << "The italian chef makes pasta" << endl;
}
// Sobrescribe el método "makeSpecialDish" de la clase padre "Chef".
void makeSpecialDish() {
cout << "The italian chef makes chicken parm" << endl;
}
};
int main() {
Book book1; // Crea una instancia u objeto de la clase Book usando el constructor vacío.
book1.title = "Harry Potter";
book1.author = "JK Rowling";
book1.pages = 500;
Book book2;
book2.title = "Lord of the Rings";
book2.author = "Tolkien";
book2.pages = 700;
Book book3("Lord of the Rings 2", "Tolkien", 710); // Usa el constructor con parámetros.
cout << book1.title << " - " << book1.author << " - " << book1.pages << endl;
cout << book2.title << " - " << book2.author << " - " << book2.pages << endl;
cout << book3.title << " - " << book3.author << " - " << book3.pages << endl;
book2.pages = 650;
cout << book2.pages;
cout << "--------------------\n";
Student student1("Jim", "Business", 2.4);
Student student2("Pam", "Art", 3.6);
cout << student1.hasHonors() << endl;
cout << student2.hasHonors() << endl;
cout << "--------------------\n";
Movie movie1("The Avengers", "Joss Whedon", "PG-13");
cout << movie1.getRating() << endl;
movie1.setRating("Dog");
cout << movie1.getRating() << endl;
cout << "--------------------\n";
Chef chef;
chef.makeChicken();
cout << "--------------------\n";
ItalianChef italianChef;
italianChef.makeChicken();
italianChef.makePasta();
italianChef.makeSpecialDish();
return 0;
}