xxxxxxxxxx
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
xxxxxxxxxx
//Example
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class Car {
//scopes(public-private)
//private: write in it the attributes of the class created
//attributes written in the private scope 5as bel class nfso
//m2drsh at3amel m3ah 8er gowa el class (m2drsh at3amel m3ah fel main)
private:
char name[15];
char color[10];
int maxSpeed;
int model;
//public: write the functions in it
//printing, declaring,...variables occur in functions
//b2dr awsalo mn ay heta (main...)
public:
//constructor
Car() {
strcpy_s(name,"No name");
strcpy_s(color,"No color");
maxSpeed=0;
model=0;
}
//Setters
void setName(char n[]) {
strcpy_s(name, n);
}
void setColor(char c[]) {
strcpy_s(color, c);
}
void setMaxSpeed(int s) {
maxSpeed = s;
}
void setModel(int m) {
model = m;
}
//Getters
char* getName() {
return name;
}
char* getColor() {
return color;
}
int getMaxSpeed() {
return maxSpeed;
}
int getModel() {
return model ;
}
//function to print
void print() {
cout << "Name: " << getName() << endl;
cout << "Color: " << getColor() << endl;
cout << "Maximum speed: " << getMaxSpeed() << endl;
cout << "Model: " << getModel() << endl;
}
};
int main() {
Car car; // Creating an object
char n[15] = "BMW";
char c[10] = "Red";
car.setModel(2022);
car.setMaxSpeed(250);
car.setName(n);
car.setColor(c);
car.print();
}
xxxxxxxxxx
class Rectangle
{
int width, height;
public:
void set_values (int,int);
int area() {return width*height;}
};
void Rectangle::set_values (int x, int y)
{
width = x;
height = y;
}
xxxxxxxxxx
class MyClass {
public:
int myNum;
string myString;
};
int main()
{
MyClass myObj;
myObj.myNum = 15;
myObj.myString = "Some text";
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}
xxxxxxxxxx
#include <iostream>
#include <string>
using namespace std;
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
int main() {
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.myNum = 15;
myObj.myString = "Some text";
// Print values
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}
xxxxxxxxxx
// classes example
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area() {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
int main () {
Rectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
xxxxxxxxxx
class SampleClass
{
int a;
public:
SampleClass(int v)
{
a=v;
}
int RetVal()
{
return a;
}
};
xxxxxxxxxx
#include <iostream>
#include <string>
class Shirt
{ // Private attributes access
// Attributes under this line can be accessed only inside of the class.
// (In the block of code with curly brackets marked with
// "Private attributes access")
private: // this line is not necessary, because fields declared
// before any access specifier are private by default
// Class field
std::string purchaseDate;
// Attributes under this line can be accessed everywhere
public:
// Class field
unsigned int sleeveLength;
// Class Method
void shortenSleeve(unsigned int n)
{
if (sleeveLength - n < 0)
{
std::cout << "Cannot shorten that much" << std::endl;
return;
}
sleeveLength -= n;
std::cout << "Sleeve Shortened successfully" << std::endl;
}
// Constructor
Shirt(std::string purchD, int sL) : purchaseDate(purchD), sleeveLength(sL)
{
}
/* Alternative
Shirt(std::string purchD, int sL)
{
purchaseDate = purchD;
sleeveLength = sL;
}
*/
}; // Private attributes access
int main()
{
Shirt JoesShirt("07.07.2022", 70);
// Alternative
// Shirt JoesShirt = Shirt("07.07.2022", 70);
// std::cout << JoesShirt.purchaseDate // Error
JoesShirt.shortenSleeve(20);
std::cout << JoesShirt.sleeveLength << std::endl;
}
/*
Output:
Sleeve Shortened successfully
50
*/