Skip to content

C) Obiektowe

kalinosia edited this page Mar 13, 2021 · 2 revisions

It’s a programming paradigm, in which​ a real-life problem​ is expressed in a form of so called objects and interactions between Objects are defined using custom blueprint type – a class.

What is class

  • A class is: data + behaviour

  • Data is given by attributes or properties, while behaviour is given by methods.

  • Classes are instantiated into objects, also known as class instances.

  • Each instance of a given class can have different state, but all of them will have same behaviour.

  • Three access modifiers available in C++:​ ​public, protected and private.

  • Public -> grants access outside of the class. All public methods and properties form an interface.​

  • Private -> accessible only by class members

  • Private / Protected -> used for data hiding or **encapsulation.​ **

class vs struct in C++​

In C++, both classes and structs play same role and can be used interchangeably. Class members are by default private, Struct members are by default public.


main.cpp

FUNCTION IN ONE / THIS FILE:

------------------------------- libraries and attachments --- 
#include<iostream>
--------------------------------- namespace or not-----------
using namespace std;
--------------------------------- function or not------------
string ask() { //not void ! because return name (string)  
	string name;
	cout << "Your name is : " << flush;
	cin >> name;
	//co_return name;
	return name;
}
void yourName(string name) {
	cout << "Your name is " << endl;
	for (int i = 0; i < name.size(); i++) {
		cout << name[i] << endl;
	}
}
----------------------------------- main loop (...) ------
int main() {

	string user = ask();
	yourName(user); 
	cout << "function sizeof: " << sizeof(user) << " function .size(): " << user.size() << endl;
	
        return 0;
}

IF FUNCTION BELOW MAIN DECLARATION BEFORE

Better to read that file

------------------------------- libraries and attachments --- 
#include<iostream>
--------------------------------- namespace or not-----------
using namespace std;
------------------------------ function declaration or not---
string ask(); //only declaration here
void yourName(string name);
---------------------------------- main loop ----------------
int main() {

	string user = ask();
	yourName(user); 
	cout << "function sizeof: " << sizeof(user) << " function .size(): " << user.size() << endl;
	
        return 0;
}
--------------------------------------------- function or not --------------
string ask() { //not void ! because return name (string)  
	string name;
	cout << "Your name is : " << flush;
	cin >> name;
	//co_return name;
	return name;
}
void yourName(string name) {
	cout << "Your name is " << endl;
	for (int i = 0; i < name.size(); i++) {
		cout << name[i] << endl;
	}

OR ONLY USE FUNCTION FROM OTHER FILE (header.h.....)

headers.h (for example Cat.h)

class Cat {            // class declaration all qualities  and function 
private:              //PRIVATE don't use in main..., use in function(...)
	bool hungry;
public:
	bool happy;
	void speak();
	void jump();

	void feed();
	void hunger();

	Cat(); //CONSTRUCTOR
	~Cat(); //DESTRUCTOR
};

second.cpp (for example Cat.cpp)

#include <iostream>
#include "headers.h"  //Class declaration (Cat.h)

using namespace std;

Cat::Cat() { //CONSTRUCTOR
	cout << "Cat created." << endl;
}
Cat::~Cat() {//DESTRUCTOR
	cout << "destroy cat" << endl;
}


void Cat::jump() {        //Function Class :: function_name(){ TODO; }
 	cout << "Jumping to top of bookcase" << endl;
}

void Cat::speak() {
	if (happy) {
		cout << "Meouwww!!!" << endl;
	}
	else {
  		cout << "Grrry!!!" << endl;
	}
}
void Cat::feed() {
 	hungry = false; //private hungry
}

main.cpp

When you have more files

#include <iostream> //libraries <...>
#include "headers.h" // files from our project "..."

using namespace std;

int main() {

	Cat cat3; //Class example_name;
	cat3.happy = true;
	cat3.feed();
	cat3.speak();
	cat3.jump();
	
        return 0;
}

Better? 👍

Clone this wiki locally