Classes in C++ - Reading Assignment

In classes we can define functions that can use the members of the class to perform actions on. We can use functions in structs too, but when we use classes, we don’t need to add the class itself to the function, like we have to when we use structs. We can simply access the class variables.

Methods are member functions of classes. They are used to perform actions on data stored in a class.

Class is a collection of variables (same or different types) and functions, that can be easily accessible, updated and performed actions on.

An object is an instantiated class or struct, that contain different types of variables.

1 Like

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

Using the struct keyword for data-only structures, and the class keyword for defining objects that require both data and functions to be bundled together.

2. What are methods?

Functions defined inside of a class are called member functions (or sometimes methods ).

3. How would you define a class?

With the Class keyword and starting with a capital letter

4. What is an object?

An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.

1 Like
  1. It useful for object-oriented programming, classes not only hold data, but provide functions that work with the data as well.
  2. Methods are functions defined inside of a class.
class ClassName {
public:
....}:
  1. It is an instance of the class.
1 Like
  1. Structs are structures that hold data only.

Classes are structures that hold both data and member functions.

  1. Methods ( member functions ) are functions defined in a class (though they can also be defined outside of that class).

  2. class ClassName {
    Access specifier; // Usually public:, but can be private or protected
    Data members; // The variables you are using (int, double, string etc)
    Member functions();
    }; // Classes end with a semicolon, just like structs

  3. An object is an instance (or variable) of a class.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Because with classes we get more functionality through functions.

  2. What are methods?
    Methods are functions inside a class. (member functions)

  3. How would you define a class?
    A class is a blueprint for what an object should look like.

  4. What is an object?
    An object is a variable of a classtype.

1 Like
  1. We need classes so we can perform functions on the data of our object.
  2. Methods are the functions defined in the given class.
  3. A class contains the variables and functions specified for a given object.
  4. An object is a data type represented by a class.
1 Like
  1. In structs you can only store data, in classes you can make functions, structs and other things.
  2. A method is a function in a class
  3. class name{}
  4. Something used to represent something
1 Like

1. Why do we need classes, why can’t we just use structs?
With structs, we can define a data type that can hold data. If we want our data type to also do something with the data, we use classes which allow us to write functions in these data types.
2. What are methods?
Methods are the functions inside a class. They are also called member functions - they are members of the class.
3. How would you define a class?
class Fruit
{
public:
int m_diameter { };
double m_weight { };
string m_color { };
void print ()
{
cout << m_color;
}
};

It is advised to add m_ in front of member names to distinguish them from other variables.
Also, note that print () can find m_color as it is in the same class. We don’t have to type fruitname.m_color. It is called the implicit object.
4. What is an object?
Here I am a bit confused. In the article, they refer to members of a class as objects, which means that in my example objects would be m_diameter, m_weight and m_color.
However, according to this article https://www.w3schools.com/cpp/cpp_classes.asp,
if I declare Fruit apple; an object would be apple.

1 Like

Yea its a bit vague definition. But in the context of classes an object is an instance of a class like you stated:
Fruit apple; // apple is an instance object of a class Fruit :slight_smile:

1 Like
  1. Because we can use methods or functions inside the class.
  2. Methods are functions inside a class.
  3. class MyClass
    {
    public:
    int m_data;
    void someFunction()
    {
    cout << “Hello \n”;
    }

};
4. The class is a “blueprint” for creating an object. When we use a class and assign data to it, then we created an object.

1 Like
  1. structs cant pass functions, where classes can

  2. functions defined inside a class

  3. class Class{
    public:
    int a;
    int b;
    int c;
    };

  4. an instance of a class

1 Like

1)Because we want to our type to not only hold data, but provide functions that work with the data as well.
2)Functions that are inside a class.
3)class DateClass
{

};
4)It is an instance of a class.

1 Like
  1. Unlike structs classes can contain associated member functions.

  2. Methods are functions defined inside of a class.

  3. Classes can be defined similar to structs by using the keyword class in the place of struct and having the public: keyword:

class ClassName {
public: 

int member 1;
string member 2;
}
  1. Object is a class type variable. Objects are also called instance of the class. Each object contains all members (variables and functions) declared in the class.
1 Like
  1. Why do we need classes, why can’t we just use structs?
    In the world of object-oriented programming, we often want our types to not only hold data, but provide functions that work with the data as well.
  2. What are methods?
    Functions defined inside of a class are called member functions (or sometimes methods).
  3. How would you define a class?
class ClassPerson
{
public:
    string m_name{};
    int m_age{};
    string m_gender{};
};
  1. What is an object?
    An object is created from a class.
    MyClass myObj;
1 Like
  1. Classes enable us to hold data and also provide functions that work with the data.

  2. Methods are functions defined inside of a class

  3. class ClassExample {

Create functions or methods in here both public and private*

};

  1. An object is an instant of a class with memory
1 Like

Thanks for posting this, Aleš! I was confused similar to Rafael21 until I read this.

1 Like

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

A class that allocates memory will deallocate it before being destroyed, but it is not safe to assume that a struct will. We should therefore use the struct keyword for data-only structures and the class keyword for defining objects that involve both data and functions together.

2. What are methods?

Functions defined inside of a class are called methods.

3. How would you define a class?

Start with the keyword “class” followed by the name you want to give to the class (remembering to name your class with a capital letter):

Example:

class Person{

public:

string name;

int age;

double wage;

// functions here

};

4. What is an object?

An instance of the class is an object, not the definition of the class itself (thanks Alko89!)

1 Like

Both classes and structs will deallocate memory fine if used correctly. The reason we use classes is so we can define methods to interact with variables inside a class :slight_smile:

2 Likes
  1. Why do we need classes, why can’t we just use structs?
    We should use classes when defining member functions. Although it’s possible to define member classes with structs, it’s not safe to assume a struct will deallocate memory after being destroyed.

  2. What are methods?
    Methods are member functions defined within the class.

  3. How would you define a class?
    It’s a method to define custom data-types that can include variables and/or functions.
    class ClassName
    {
    public:
    //Data elements defined here
    int element1;
    char element2;
    void functionX {
    // do something here
    }
    };

  4. What is an object?
    An object is a piece of memory with values stored and specific proprieties and behaviors defined.

1 Like

1. Why do we need classes, why can’t we just use structs?
we should use the struct keyword for data-only structures, and the class keyword for defining objects that require both data and functions to be bundled together.

2. What are methods? functions defined inside class
3. How would you define a class?

class DateClass
{
public:

};

***4. What is an object?***A variable of the type of class

1 Like