0% found this document useful (0 votes)
22 views4 pages

OOP Concepts and Class Definitions in C++

OOP Translation

Uploaded by

justemirz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

OOP Concepts and Class Definitions in C++

OOP Translation

Uploaded by

justemirz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

16.

1 Object-Oriented Programming (OOP)

Classes are used. Within a class, there are data members and member functions.

- Data Members: Information belonging to the object (e.g., name, age)

- Member Functions: Operations like print, calculate

- Object: An instance of a class. It occupies memory.

!!!!

In C++, OOP increases code reusability.

The code becomes modular: each object carries its own responsibility.

Main principles of OOP:

1. Encapsulation: Preventing direct access to data, using setters/getters instead

2. Inheritance: A class inherits properties and methods from another class, reducing code repetition

3. Polymorphism: The same method name can function differently in various classes

4. Abstraction: Highlighting only essential features of a complex system/object, ignoring other details

- Function declaration: class Car { void start(); }; (Does not include a body since the result is not defined yet!)

- Definition: void write() { cout << "Hello"; } (Includes a body - result is "Hello")

To include a body: You need curly braces + a defined outcome

16.2 Defining a Class with Member Functions

class Car {

public:

void start() {

cout << "Car started!" << endl;

};
public defines elements accessible from outside.

Car bmw;

[Link](); // Output: "Car started!"

Functions defined inside the class can be used directly.

The semicolon ; at the end of a class definition is mandatory.

16.3 Defining a Member Function with a Parameter

- Member function with parameter: void write(int);

- Functions can take input (parameters) to be more flexible:

class Student {

public:

void greet(string name); // Declaration only

};

// Defined outside the class

void Student::greet(string name) {

cout << "Hello, " << name << "!" << endl;

This structure is preferred in large projects for better code organization.

16.4 Data Members, Set and Get Functions

Data Member: Variables defined inside a class, usually private:

class Student {

private:

string name;

};
Set and Get Usage:

Student stu;

[Link]("Ayse");

cout << [Link](); // Output: "Ayse"

Why private + set/get?

- Ensures encapsulation: Data cannot be directly modified, giving us control

- Protects against invalid data input

16.5 Constructors

- Run automatically, are assigned automatically

- Constructor name must be the same as the class

- No return type (no void, int, etc. DO NOT WRITE!)

- Constructors can be overloaded:

class Car {

public:

Car() { cout << "Default\n"; }

Car(string brand) { cout << "Brand: " << brand << endl; }

};

16.6 Placing Class in a Separate File

1. Header (.h) file = Declares the class. Written at the top.

2. Source (.cpp) file = Defines the functions. Written at the bottom.

Advantages:

- Improves code readability

- Reduces code duplication

- Facilitates teamwork

You might also like