Search
 
SCRIPT & CODE EXAMPLE
 

CPP

classes and objects in c++

//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();
}
Comment

how to write a class in c++

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;
}
Comment

c++ classes

class MyClass {
  public:
    int myNum;
    string myString;
};

int main()
{
  MyClass myObj;

  myObj.myNum = 15;
  myObj.myString = "Some text";

  cout << myObj.myNum << "
";
  cout << myObj.myString;
  return 0;
}
Comment

C++ classes

#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 << "
"; 
  cout << myObj.myString; 
  return 0;
}
Comment

C++ classes

// Base class
class Animal {
  public:
    void animalSound() {
    cout << "The animal makes a sound 
" ;
  }
};

// Derived class
class Dog : public Animal {
  public:
    void animalSound() {
    cout << "The dog says: bow wow 
" ;
  }
}; 

int main() {
  Animal myAnimal;
  Dog myDog;

  myAnimal.animalSound();
  myDog.animalSound();
  return 0;
} 
Comment

c++ Class

// 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;
}
Comment

c++ classes

class SampleClass
{
    int a;
public:
    SampleClass(int v)
    {
        a=v;
    }
    int RetVal()
    {
        return a;
    }
};
Comment

C++ classes

// Base class
class Vehicle {
  public:
    string brand = "Ford";
    void honk() {
      cout << "Tuut, tuut! 
" ;
    }
};

// Derived class
class Car: public Vehicle {
  public:
    string model = "Mustang";
};

int main() {
  Car myCar;
  myCar.honk();
  cout << myCar.brand + " " + myCar.model;
  return 0;
} 
Comment

declare class c++

#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
*/
Comment

C++ Classes

class MyClass {       // The class
  public:             // Access specifier
    int myNum;        // Attribute (int variable)
    string myString;  // Attribute (string variable)
};
Comment

PREVIOUS NEXT
Code Example
Cpp :: pow without math.h 
Cpp :: c++ swap function 
Cpp :: Accessing C++ Array Elements 
Cpp :: unordered map c++ 
Cpp :: ifstream 
Cpp :: c++ if else example 
Cpp :: or in c++ 
Cpp :: c++ class methods 
Cpp :: right shift in c++ 
Cpp :: nested conditional operator 
Cpp :: how we can write code to remove a character in c++ 
Cpp :: Valid Parentheses leetcode in c++ 
Cpp :: floor and ceil in cpp 
Cpp :: c++ unordered_map initialize new value 
Cpp :: use declaration to define a variable 
Cpp :: what destructor used for in c++ 
Cpp :: create a copy of a vector c++ 
Cpp :: rgb type def 
Cpp :: flipperRSocketResponder.cpp command failed react native 
Cpp :: pointers in cpp 
Cpp :: curl upload folder and subfolders 
Cpp :: c++ error missing terminating character 
Cpp :: cap phat dong mang 2 chieu trong c++ 
Cpp :: can i delete a null pointer in c++ 
Cpp :: class how to call main method inheritance in c++ 
Cpp :: gcd multi num 
Cpp :: c++ iterator shorthand 
Cpp :: c++ to mips assembly converter 
Cpp :: split 2d array into chunks in c++ 
Cpp :: c++ freecodecamp course 10 hours youtube 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =