Lesson 2.1 Classes and Objects

Cards (11)

  • In C++, everything is associated with classes and objects, along with their attributes and methods
  • In real life, a car can be considered an object with attributes like weight and color, and methods like drive and brake
  • Attributes and methods in classes are essentially variables and functions that belong to the class, often referred to as "class members"
  • A class is a user-defined data type that serves as a blueprint for creating objects
  • To create a class in C++, use the class keyword
  • Example:
    • Create a class named "MyClass" with attributes myNum (int variable) and myString (string variable)
  • An object in C++ is created from a class
  • To create an object of a class like MyClass, specify the class name followed by the object name
  • To access class attributes (like myNum and myString), use the dot syntax (.) on the object
  • Example:
    • Create an object named "myObj" and access its attributes
  • In the provided code snippet:
    • An object of MyClass named myObj is created
    • The attributes myNum is set to 15 and myString is set to "Some text"
    • The attribute values are printed using cout