Object-oriented programming (OOP) is a programming paradigm that enables developers to create modular, reusable, and extensible software by organizing code into objects that encapsulate data and behavior.
In C++, objects are created by defining classes, which are blueprints for creating objects with specific attributes and methods. These objects can then be used to perform various tasks, such as running external applications, performing string operations, and manipulating different data types.
One of the key benefits of object-oriented programming is that it helps developers manage complex codebases by reducing code duplication, improving code organization, and making it easier to maintain and update code over time. Additionally, OOP provides encapsulation, inheritance, and polymorphism, which are powerful mechanisms for creating modular and reusable code.
#include <iostream>
#include <cstring>
using namespace std;
void getStuff(struct Books *genField);
struct Books {
char Title[50];
char Author[50];
char Topic[100];
int id_book;
};
int main() {
struct Books Book_1;
strcpy(Book_1.Title, "Relativity T");
strcpy(Book_1.Author, "Albert E");
strcpy(Book_1.Topic, "Relativity");
Book_1.id_book = 4323;
getStuff(&Book_1);
return 0;
}
void getStuff(struct Books *genField) {
cout << "Book_1 - Title: \t" << genField->Title << endl;
cout << "Book_1 - Author: \t" << genField->Author << endl;
cout << "Book_1 - Topic: \t" << genField->Topic << endl;
cout << "Book_1 - ID: \t" << genField->id_book << endl;
cout << "-------------------------------------" << endl;
}
This code defines a C++ structure named "Books" that contains four member variables: Title, Author, Topic, and id_book. Then, the main function creates an object of this structure named "Book_1" and initializes its member variables using the "strcpy" function.
After that, the main function calls the "getStuff" function with a pointer to the "Book_1" object. The "getStuff" function takes a pointer to a "Books" structure as its parameter and prints the values of the structure's member variables to the console using the "->" operator.
In summary, this code demonstrates the use of C++ structures to define a blueprint for creating objects with specific properties and methods. It also shows how to initialize and manipulate the member variables of a structure and how to pass a pointer to a structure to a function for further manipulation. Overall, this code provides a good introduction to C++ structures and how they can be used in programming.
Please note, we are using this structure example as nice base for class understanding. Structures are form of blueprint, and we will take that knowledge when thinking about classes.
#include <iostream>
using namespace std;
class specName{
public:
void printSomething() {
cout << "Hack The Planet !" << endl;
}
};
int main() {
return 0;
}
Class creation:
#include <iostream>
using namespace std;
class specName{
public:
void printSomething() {
cout << "Hack The Planet !" << endl;
}
};
int main() {
return 0;
}
This code defines a C++ class named "specName" that has one member function named "printSomething". The "printSomething" function simply prints the string "Hack The Planet!" to the console when called.
The "main" function is empty and doesn't do anything. It simply returns 0 at the end, which indicates a successful execution of the program.
Overall, this code demonstrates the basics of defining and using a C++ class, and how to define a member function and call it from the main function. However, it doesn't really show any practical use of classes, and the "printSomething" function doesn't take any arguments or return any values, so it's just a simple example to demonstrate the syntax and structure of a class.
Why we need to use "class" keyword and what is "public:"
The "class" keyword is used to define a user-defined data type that encapsulates data and functions into a single entity. The class provides a blueprint or a template for creating objects of that type. By defining a class, we can encapsulate data and functions together, and hide the implementation details from the user.
The "public:" keyword is used to specify the access level of the member variables and functions in a class. In this code, the "public:" keyword is used to declare that the member function "printSomething" is accessible from outside the class. This means that other parts of the program can call this function and use it to perform some operation.
By using the "public:" keyword, we can explicitly declare that certain members are accessible from outside the class, which can be useful for creating well-structured and modular code.
No comments:
Post a Comment