Object-Oriented Programming
Object-oriented design technique using C++ Class
Contents
Priciples
- Encapsulation (μΊ‘μν)
- Hiding implementation details, only exposing the βpublic interfaceβ
- Abstraction (μΆμν)
- Selecting data to show only the relevant details to the object
- Inheritance (μμμ±)
- The ability to acquire some/all properties of another object
- Polymorphism (λ€νμ±)
- Provision of a single interface to entities of different types
Goals
- Robustness
- We want software to be capable of handling unexpected inputs that are not explictly defined for its application.
- Adaptabillity (β Portabillity)
- Software needs to be able to evolve over time in response to changing conditions in its environment.
- Reusabillity
- The same code should be usable as a component of different systems in various applications.
Object-Oriented Software Design
- Responsibilities
- Divide the work into different actors, each with a different responsibility.
- Independence
- Define the work for each class to be as independent from other classes as possible.
- Behaviors
- Define the behaviors for each class carefully and precisely, so that the consequences of each action performed by a class will be understood by classes that interact with it.
Defining and Using a Class
class ClassName {
Access specifier:
Data members;
Member functions() { }
};
Access Specifiers
class MyClass {
public: // public access specifier
MyClass(); // constructor
int x; // public attribute
private: // private access specifier
int y; // private attribute
protected: // protected access specifier
int z; // protected attribute
};
- public
- Members are aceessible from outside the class.
- private
- Members cannot be accessed (or viewed) from outside the class. (λ
립μ±μ λμ΄λ μ€κ³)
- protected
- Members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
Example
class Student {
public:
string name;
int id;
float gpa;
void outputInfo();
private:
bool scholarship;
void calcGPA();
};
Student summaCumLaude
- Syntax :
class_name instance_name;
- Variable
summaCumLaude
is an instance of the class Student
.
- It is an object of the type
Student
.