C++ GETTERS & SETTERS explained easy 🔒

preview_player
Показать описание
#getter #setter

C++ abstraction with getters and setters tutorial example explained
Рекомендации по теме
Комментарии
Автор

#include <iostream>

class Stove{
private:
int temperature = 0;
public:

int getTemperature(){
return temperature;
}
void setTemperature(int temperature){
if(temperature < 0){
this->temperature = 0;
}
else if(temperature >= 10){
this->temperature = 10;
}
else{
this->temperature = temperature;
}
}
};

int main() {

Stove stove;

stove.setTemperature(5);

std::cout << "The temperature setting is: " << stove.getTemperature();

return 0;
}

BroCodez
Автор

Magic just happened in front of my eyes. Thank you for making it easy and understandable.

naboulsikhalid
Автор

//Volume adjuster program using getters & setters in c++
//Getters and setters allow you to enforce certain rules or conditions on the data being accessed or modified.
# include<iostream>
using namespace std;

class sony
{
private:
int volume;
public:
sony(int avolume)
{
setVolume(avolume);
}
int getVolume()
{
return volume;
}
void setVolume(int avolume)
{
if(avolume>50)
{
volume=50;
}
else if(avolume<0)
{
volume=0;
}
else
{
volume=avolume;
}
}
};

int main()
{

sony shaker(0);
int temp;
cout<<"Current
cout<<endl;
cout<<"Enter the volume change:";
cin>>temp;
shaker.setVolume(temp);
cout<<"Volume is now:"<<shaker.getVolume();

return 0;
}

JackEvans-hfqt
Автор

Class Methods Get and Sets
Ctor is setting values in the Class methods
vs
internal int Age (get; set init); <-- a class field

Dazza_Doo
Автор

Bro i really need Angular Framework bro help

minmyatthukha
Автор

Hi, Is that the complete series for c++? Is there any missing concept?

Hazar-btnf
Автор

#include <iostream>

class Pan{
private:
int temperature = 19;
public:
Pan(int temperature){
setTemperature(temperature);
}
int getTemperature(){

return temperature;
}
void setTemperature(int temperature){
if(temperature >= 10){
this->temperature = 10;
}
else if(temperature <= 0){
this->temperature = 0;
}
else{
this->temperature = temperature;
}
}
};

int main(){

Pan pan1(1 0);

//pan1.setTemperature(97);

std::cout << pan1.getTemperature();


return 0;
}

danaildoganov
Автор

#include <iostream>

class Age
{
private: // makes the members read-only and can only be modified using setters
int age = 0;

public:
std::string name;

Age()
{
}

Age(std::string _name)
{
setName(_name);
}

Age(std::string _name, int _age)
{
setName(_name);
setAge(_age);
}

std::string getName()
{
return name;
}

void setName(std::string _name) // setter
{
this->name = _name;
}

int getAge() // getter
{
return age;
}

void setAge(int _age)
{
if (_age < 0)
{
std::cout << "Invalid age! Age cannot be negative. Age has been reset for " << name << ".\n";
this->age = 0;
}
else if (_age < 18)
{
std::cout << name << " cannot modify without adult supervision.\n";
}
else if (_age < 100)
{
std::cout << "Age updated for " << name << "\n";
this->age = _age;
}
else
{
std::cout << "Invalid age! Age is too high. Age has been reset for " << name << ".\n";
this->age = 0;
}
}

void showDetails()
{
if (name != "")
{
std::cout << "Name: "<< getName() << "\n";
getName();
}
else
{
std::cout << "No data entered";
}

if (age != 0)
{
std::cout << "Age: "<< getAge() << "\n";
}
else
{
std::cout << "No age entered";
}
}
};

int main()
{
std::string name;
Age rec;
int age;
int ch;

do
{
std::cout <<
std::cout << "1. Enter details\n2. Update age\n3. Show Details\n4. Exit\nEnter choice (1/2/3/4): ";
std::cin >> ch;
std::cout <<

if (ch == 1)
{
std::cout << "Enter name: ";
std::cin >> name;
std::cout << "Enter age: ";
std::cin >> age;
rec.name = name;
rec.setAge(age);
std::cout << "Details entered!\n";
}

else if (ch == 2)
{
std::cout << "Enter age: ";
std::cin >> age;
rec.setAge(age);
}

else if (ch == 3)
{
rec.showDetails();
}

else if (ch == 4)
{
std::cout << "Thank you!\n";
}

else
{
std::cout << "Invalid choice!\n";
}
} while (ch != 4);

return 0;
}

lyfisntdaijobu
Автор

#include <iostream>

class human{
private:

std::string name = "null";
int age =0;




public:
void setName(std::string name){
this->name=name;
}
void setAge(int age){
this->age=age;
}
std::string getName(){
return name;
}

int getAge(){
return age;
}
};

int main()
{
human h1;
h1.setName("sam");
h1.setAge(20);

std::cout << "Your Name is : " << h1.getName() << '\n'
<< "And " << "Age is : " << h1.getAge() ;
}

AhmadAbubakar-wf