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 :: c++ convert int to cstring 
Cpp :: print fps sfml 
Cpp :: for statement c++ 
Cpp :: c++ count vector elements 
Cpp :: power in c++ 
Cpp :: what is a variable in cpp 
Cpp :: set iterator 
Cpp :: how to fill vector from inputs c++ 
Cpp :: what is the meaning of life and everything in the universe 
Cpp :: age in days in c++ 
Cpp :: recursive factorial of a number 
Cpp :: bit++ codeforces in c++ 
Cpp :: c ++ program to insert into hashmap 
Cpp :: statements 
Cpp :: clear map in C++ 
Cpp :: c++ write string 
Cpp :: string copy in cpp 
Cpp :: count c++ 
Cpp :: c++ class constructor variable arguments 
Cpp :: A Program to check if strings are rotations of each other or not 
Cpp :: print all subsequences 
Cpp :: ? c++ 
Cpp :: c++ overloading by ref-qualifiers 
Cpp :: expresiones regulares español 
Cpp :: pointers in cpp 
Cpp :: memset array bool 
Cpp :: kadane algo 
Cpp :: how to code a game in c++ 
Cpp :: c to c++ code converter 
Cpp :: softwareegg.courses4u 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =