Classes in C++ - Reading Assignment

  1. Why do we need classes, why can’t we just use structs?
    Ans: We need classes because we often want our types to not only hold data,
    but provide functions that work with the data as well. In C++, this is typically done via the class keyword. Structs can only hold data.

  2. What are methods?
    Ans: Functions defined inside of a class are called member functions (or sometimes methods).

  3. How would you define a class?
    Ans: Classes are defined very similar to structs. Here is a simple example:
    class DateClass
    {
    public:
    int m_year{};
    int m_month{};
    int m_day{};
    };

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

1 Like
  1. We use classes when we need to have member variables AND member functions. Structs should be used only for data structures.
  2. Methods are functions declared inside of a class, which has an implicit object when called upon.
  3. A class is a data type that aggregates variables and functions into an object, for more robust and modular programming organization.
  4. An object in the traditional sense is just a part of the memory to store values. In object-oriented programming, the term object refers to a more complex organized set of data and even functions, bundled into one “object”.
2 Likes
  1. Classes are similar to structs but classes also have functions. Structs can have functions too but its a bit tough because structs dont clean up after themselves and that may cause issues.

  2. Methods are the functions defined in a class.

3.A class is a structure with functinos :))

  1. An object is the result of initializing a class. Basically a structure with functions. A structure with functions seem to be the answer to most of this assignment’s questions :))
1 Like

Kind of, but the object is an instance of the structure with functions based on the class definition :slight_smile:

  1. Why do we need classes, why can’t we just use structs? Basically a class is a struct that contains functions as members. This is not possible with common structs. This will allow to manage data and data-related functionality together.

  2. What are methods? Methods are functions or “member functions” defined in classes

  3. How would you define a class?
    class classname {

    };

  4. What is an object? A variable, or instance, or a class

1 Like

1. Why do we need classes, why can’t we just use structs?
for object oriented programming we usually want our types to not only hold data but provide functions that work with the data.
2. What are methods?
Functions defined inside a class.
3. How would you define a class?
class DateClass
{
public:
int m_year{};
int m_month{};
int m_day{};

void print()
{
std::cout << m_year << ‘/’ << m_month << ‘/’ << m_day;
}
};
4. What is an object?
An object is an instance of a class.

1 Like
  1. Classes hold variables and functions while structs are data only
  2. Methods are member fields with classes
  3. Classes can hold data and do functions with the data
  4. Objects are class members
1 Like
  1. Why do we need classes, why can’t we just use structs?

  2. What are methods?

  3. How would you define a class?

To define class, you start with keyword “class”, then continue with name that you want to give to the class, then open {} brackets and inside them you start with keyword “public:” and then define members in it by using any of the keywords “int”, “double” etc., after the keyword you write your name of the member adding {} brackets after then and semicolon. It is better to name member in class starting with “m_…”. After the class brackets semicolon is mandatory. Example:

class DateClass

{

public:

int m_year{};

int m_month{};

int m_day{};

};

  1. An object is an instance of the class
2 Likes
  1. We need classes because they give us more flexibility in the sense that they allow data structures as well as functions. Structs also can have functions but classes handle them better as they were specifically designed to do so.

  2. Methods are functions that are located within classes (or objects).

  3. I would define a class as an object type that is made to hold methods inside of it.

  4. An object is something that stores information inside of it.

2 Likes

A class definition is not yet an object, an instance of that class is. :slight_smile:

I’ve been struggling with this concept but maybe I’m overthinking it. So a class definition is basically just a definition or type. But it’s not an object until it an instance of it is used. Is this correct?

A definition yes, kind of like a blueprint of the class. :slight_smile: an object is when you create a variable of the class type.

1 Like

Ans 1:
We need classes because they hold and store data too, just like a struct, but more importantly, they can also contain functions that can execute the struct / class construct.

Ans 2:
Methods, or more commonly known as ‘member functions’, are the parameters, statements or variable functionality of that particular struct / class program.

Ans 3:
A class is a nestled part of program language, a method or a member that represents functionality of the struct from which it is called and executed from.

Ans 4:
An object is a name that is given as a target for a class. In the instance, that object is called, it directly references that objects memory allocation.

1 Like
  1. Classes can clean up memory usage after themselves, so where you need a type of object that comes with its own functions then classes are recommended as structs do not clean up memory usage.
  2. Methods are functions within a class.
  3. A class is a data structure that can be mulltiply instantiated and holds relevant member functions (methods) and variables.
  4. An object is a piece of data.
1 Like
  1. Because we want our types to 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 defined by using the keyword “class” followed by the name of the class. The body of the class is defined inside the curly brackets and terminated by a semicolon.

  4. An object is an instance of a class. When a class is defined no memory is allocated, but when it is instantiated (an object is created) memory is alocated.

2 Likes
  1. If we need types that contain data types as well as functions that wok with the data.
  2. Methods (or member functions) are functions defined inside of a class.
class ClassName {
    public:
	// public members and methods
	private:
	// private members and methods
};
  1. An instance of a class
1 Like
  1. Why do we need classes, why can’t we just use structs? Structs
    are only for holding data, but classes can hold data and use functions.

  2. What are methods? Methods are the funtions that classes use.

  3. How would you define a class?
    Right after the include statements near the top of the program.
    For example:
    Class DateClass

  4. What is an object?
    In object-oriented programming languages like C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object.

1 Like
  1. We need classes which has a keyword public to store function. Struct only stores data information. No function & no code inside struct!

  2. Functions defined inside of a class are called member functions (or sometimes methods ). Member functions can be defined inside or outside of the class definition.

  3. The class keyword defines a new user-defined type called a class.

:christmas_tree: :balloon:

2 Likes
  1. Why do we need classes, why can’t we just use structs?
    Structs can only hold data. If we want an object to not only hold data but have functions as well, classes are needed.

  2. What are methods?
    A method is a function defined inside a class.

  3. How would you define a class?
    class MyClass
    {
    private:
    // private members
    public:
    // public members
    };

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

1 Like

Classes in C++ - Reading Assignment.

  1. We use classes instead of structs because structs are for data structures only ( only hold data, and do not have associated member functions), while classes define objects that require both data and functions to be bundled together.

  2. Methods are functions defined inside of a class. They can be private, public, or protected.

  3. Class is a keyword that can contain both member variables and member functions bundled together.

  4. An object is an instantiation of a class. In terms of variable, a class would be the type and an object would be the variable.

1 Like