Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Why use Object-Oriented Programming? | Fundamentals of OOP in C++
C++ OOP
course content

Conteúdo do Curso

C++ OOP

C++ OOP

1. Fundamentals of OOP in C++
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
Why use Object-Oriented Programming?

Imagine you were given a task to develop a software application to manage student data. It might seem straightforward at first. You might begin by using simple variables to store a student’s name and GPA.

cpp

main

copy
12345
int main() { std::string student_name = "Bob"; float grade_point_average = 3.3; }

This approach works fine for managing a single student, but things get more complex with multiple students. A naive solution might use separate variables for each one, but this quickly becomes unmanageable. Using arrays is a better option to reduce repetition and improve organization.

cpp

main

copy
12345
int main() { std::string names[2] { "Bob", "Ann" }; float avarage_grades[2] { 3.3, 3.7 }; }

This works for now. But what if you need to store more details, such as phone numbers, enrollment dates, or course lists? As the data grows, managing multiple separate arrays becomes cumbersome and error-prone.

cpp

main

copy
123456
int main() { std::string names[2] { "Bob", "Ann" }; float average_grades[2] { 3.3f, 3.7f }; std::string emails[2] { "bob@example.com", "ann@example.com" }; }

To manage this data, you might create functions that take these values as arguments to perform actions like printing student info, updating the GPA, or changing the email address. But with multiple students and fields, you end up repeating the same arguments across many functions, making the code error-prone and hard to maintain.

h

functions

copy
123456
void get_student_info(int id, std::string names[], float gpa[], std::string emails[]); void set_student_info(int id, std::string names[], float gpa[], std::string emails[]); void set_student_name(int id, std::string names[], float gpa[], std::string emails[]); void set_student_gpa(int id, std::string names[], float gpa[], std::string emails[]); void set_student_email(int id, std::string names[], float gpa[], std::string emails[]);

Even simple tasks require passing the same set of arrays repeatedly. As you add more fields, the code becomes increasingly complex and repetitive.

Object-Oriented Programming (OOP) solves this by allowing you to group related data and behavior into a single container called a class. This not only simplifies your code, but also improves encapsulation by controlling access to internal details through clear, well-defined interfaces.

question mark

Which of the following is the main advantage of using object-oriented programming?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

course content

Conteúdo do Curso

C++ OOP

C++ OOP

1. Fundamentals of OOP in C++
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
Why use Object-Oriented Programming?

Imagine you were given a task to develop a software application to manage student data. It might seem straightforward at first. You might begin by using simple variables to store a student’s name and GPA.

cpp

main

copy
12345
int main() { std::string student_name = "Bob"; float grade_point_average = 3.3; }

This approach works fine for managing a single student, but things get more complex with multiple students. A naive solution might use separate variables for each one, but this quickly becomes unmanageable. Using arrays is a better option to reduce repetition and improve organization.

cpp

main

copy
12345
int main() { std::string names[2] { "Bob", "Ann" }; float avarage_grades[2] { 3.3, 3.7 }; }

This works for now. But what if you need to store more details, such as phone numbers, enrollment dates, or course lists? As the data grows, managing multiple separate arrays becomes cumbersome and error-prone.

cpp

main

copy
123456
int main() { std::string names[2] { "Bob", "Ann" }; float average_grades[2] { 3.3f, 3.7f }; std::string emails[2] { "bob@example.com", "ann@example.com" }; }

To manage this data, you might create functions that take these values as arguments to perform actions like printing student info, updating the GPA, or changing the email address. But with multiple students and fields, you end up repeating the same arguments across many functions, making the code error-prone and hard to maintain.

h

functions

copy
123456
void get_student_info(int id, std::string names[], float gpa[], std::string emails[]); void set_student_info(int id, std::string names[], float gpa[], std::string emails[]); void set_student_name(int id, std::string names[], float gpa[], std::string emails[]); void set_student_gpa(int id, std::string names[], float gpa[], std::string emails[]); void set_student_email(int id, std::string names[], float gpa[], std::string emails[]);

Even simple tasks require passing the same set of arrays repeatedly. As you add more fields, the code becomes increasingly complex and repetitive.

Object-Oriented Programming (OOP) solves this by allowing you to group related data and behavior into a single container called a class. This not only simplifies your code, but also improves encapsulation by controlling access to internal details through clear, well-defined interfaces.

question mark

Which of the following is the main advantage of using object-oriented programming?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2
Sentimos muito que algo saiu errado. O que aconteceu?
some-alt