Advertisement
#include<iostream>
using namespace std;
class A
{
public: // public - visibility label
int weight; // weight with public visibility label makes it accessible by anyone
};
int main()
{
A ob;
ob.weight = -100; //weight set to a negative value, legal but wrong.
cout<<"weight is : " << ob.weight;
}
weight is : -100
Advertisement
#include<iostream>
using namespace std;
class A
{
private:
int weight; //declaring weight with private visibility label
public:
int getWeight() //public getter method to get weight.
{
return weight;
}
void setWeight(int wt) //public setter method to set weight.
{
if(wt<0)
cout<<"Please enter weight greater than zero";
else
weight=wt;
}
};
int main()
{
A ob;
ob.setWeight(100);
}
#include<iostream>
using namespace std;
//Abstract class food
class food
{
public:
virtual void cook() = 0;
};
//Class Soup inherits the abstract class food
class Soup : public food
{
public:
void cook()
{
cout<<"Cooking Soup";
}
};
int main()
{
Soup ob;
ob.cook();
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement