Classes in C++ - Reading Assignment

  1. Why do we need classes, why can’t we just use structs? Because with functions you can work with data… where structs is just data.
  2. What are methods? They are methods defined as classes
  3. How would you define a class? data structure that enables you to create objects and is the core of object oriented programming.
  4. What is an object? data type that is defined by a given class
1 Like

1. Why do we need classes, why can’t we just use structs?
Classes allow for the use of functions while structs only store data

2. What are methods?
Methods are functions defined within a class

3. How would you define a class?
class ClassName{…}

4. What is an object?
a variable of a class type

1 Like

1.So that functions work with the data.
2. Methods are functions inside of classes.
3. class DateClass
{
public:
int m_year{};
int m_month{};
int m_day{};
4. An object is an instance of a class.

1 Like
  1. Because we can’t use methods in structs.
  2. A method is a function in a class.
  3. class Car {
    public:
    string m_brand{};
    int kilometers{};

    };
  4. Its an instance of a class. for my example: Car lamborghini{“Lamborghini”, 83000, …} ;
1 Like
  1. Classes allow us to write member functions that use the member variables.
  2. Are similar to functions
  3. class ClassName { Public: variable; [optional Function] }
  4. Instance of a class.
1 Like
  1. Because you can use function within a class.
  2. Functions defined inside of a class.
  3. A method who allow you to create your own data types. It is basically the same as struct.
    4.It is a stored data that has a value and other associated properties.
1 Like
  1. Structs only store data, so we need classes to code functions.

  2. Method are functions defined inside of a class.

  3. Define a class by using the class keyword and including its members within curly braces.
    class MyClass {
    public:
    //variables
    //functions
    private:
    }

  4. An object aka instance is a variable of a class type.

1 Like
  1. Because using classes we can not only hold data, but provide functions that work with the data as well.
  2. Methods are functions inside of classes.
  3. A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end.
    Inside the brackets we have to add the access specifiers (public/private/protected) and members (variables and functions).

Example:

class DateClass
{
public:

// variables:
int m_year{};
int m_month{};
int m_day{};

// function:

void print() // defines a member function named print()
{
    std::cout << m_year << '/' << m_month << '/' << m_day;
}

};

  1. An object is an instance of a class.
1 Like
  1. Structs are limited to only data, while classes can be used for objects that involve both data and functions to manipulate the data.
  2. Methods are functions that are defined in a class and designed to be used to manipulate the objects/data created by the class.
  3. A class is essentially a struct that is also able to include methods.
  4. An object is a set of inter-related variables defined by a common class, sharing the same reference.
1 Like
  1. Classes work better for using member functions.
  2. A method is a member function.
  3. A class is a user-defined type that can contain data and methods.
  4. An object is an instantiation of a class.
1 Like
  1. Classes allow us to define internal/implicit class functions/methods, allowing greater functionality than structs(which only contain data members and not member functions).
  2. Functions defined inside a class.
  3. class ClassName
    {
    access specifier:
    data members;
    member functions();
    };
  4. An instance of a class.
1 Like
  1. Why do we need classes, why can’t we just use structs?
    Although structs can hold both data and functions in C++, it is better to use the struct keyword for data-only structures and use the class keyword for objects that have both data and functions.

  2. What are methods?
    Functions that are defined inside of a class.

  3. How would you define a class?
    With the class keyword and naming our class starting with capital letter.

  4. What is an object?
    An instance of a class.

1 Like

1.Classes allow us to define data and functions relating to that data in the same container. With structs we cannot do this and must define our functions separately.
2. Methods are function defined inside of a class. Methods are also called member functions.
3. We define classes in a very similar way to defining structs. The main difference is the access modifiers like public, private, etc.
class Test{
public:
int m_x{};
int m_y{};
int add(){
return m_x+m_y;
}}
4. An object is an instance of a class. When we define a class it is a blue print for an object. Each time we instantiate that class, we create an object.

1 Like
  1. We often want our types to not only hold data, but provide functions that work with the data as well.
  2. Methods (or member functions) are functions defined inside of a class.
  3. By declaring a variable
  4. Object is an instance (variable) of the class
1 Like

1. Why do we need classes, why can’t we just use structs?

STRUCTS don’t have member functions.

2. What are methods?

member functions - functions inside the classs

3. How would you define a class?

class RobloxUser{
public:
std::string m_name{};
int m_Roblox_id{};

};

4. What is an object?

an object is an instance of a class. example:

RobloxUser grant {“Grant”,112233};
RobloxUser owen {“Owen”, 11443321222};

1 Like
  1. data and function work together and structs just store data
  2. A function in a class
  3. class NameOfClass { variable or methods};
  4. a variable of a class type
1 Like

1. Why do we need classes, why can’t we just use structs?
Classes are automatically set to private and most importantly is that you have the ability to create member functions!
2. What are methods?
Functions that are within a current a class.
3. How would you define a class?
A data structure that enables you to create objects and is the core of object oriented programming. class ClassName{…}
4. What is an object?
A data type that is defined by a given class.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Structs should be used for data-only structures, and the class keyword for defining objects that require both data and functions to be bundled together. The reason for this is structs may not deallocate memory that was stored while I class would deallocate that memory.
  2. What are methods?
    Methods are functions defined inside of a class.
  3. How would you define a class?
    Using the keyword class, you can define an object which has member variables and member functions.

class weather
{
public:
int temperature{};
Boolean cloudy{};
string precipitation{};
};

  1. What is an object?
    struct keyword for data-only structures, and the class keyword for defining objects that require both data and functions to be bundled together.
1 Like
  1. Why do we need classes, why can’t we just use structs?
  • To hold data and have working functions with it.
  1. What are methods?
  • Can be functions inside a class
  1. How would you define a class?
  • Blueprint
  1. What is an object?
  • Can be a class
1 Like

1. because a class object can not only hold data but provide functions that work with the data as well.
2. functions defined inside of a class are called member functions or methods
3. A class is like a “box” containing a lot of objects basically.
4. an object is a variable containing a value.

1 Like