Classes in C++ - Reading Assignment

Welcome to the discussion about the reading assignment about Classes in C++.

Leave your answers to the questions below in this thread. If you have any questions or you want to discuss something connected to the assignment feel free to do it in this thread as well, but please everything to the topic.

1. Why do we need classes, why can’t we just use structs?
2. What are methods?
3. How would you define a class?
4. What is an object?

15 Likes

1. Why do we need classes, why can’t we just use structs?
With structs, we can only store data. With classes we can provide functions that works with data.

2. What are methods?
Methods are functions defined in classes.

3. How would you define a class?
class MyClass {
...
};

4. What is an object?
Many things : Instance of class, variable with members …

6 Likes
  1. Because structs cannot contain functions as members. We want to be able to group data and data-related functionality together.
  2. Methods are member functions of a class
  3. class myClass {
    public:
    //public member variables and member functions
    }
  4. An object is an instance of a class
1 Like
  1. to encapsulate variables and functions into a single object, structs are data only
  2. methods are functions that are part of the class and can be called on the instance
class ClassName {
  public:
  // public members and methods
  private:
  // private members and methods
}
  1. An instance of a class
3 Likes
  1. Why do we need classes, why can’t we just use structs?
    Beside describing attributes of an object we may want to describe also specific behavior of that object. Classes allow us to store data and functions within a single variable.
  2. What are methods?
    Functions that are member of a class.
  3. How would you define a class?
    class {
    //access specifiers
    // variables
    // methods
    };
  4. What is an object?
    An object is an instance of a class. A variable of the type class allocated to memory.
1 Like

Why do we need classes, why can’t we just use structs?
Because structs do not allow functions, classes are more powerful and versatile.

What are methods?
The functions inside a class.

How would you define a class?
A class is a powerful and versatile method accessed by the applications main method.(PLEASE CORRECT IF INACCURATE).
class{
functions;
objects;
//access to other classes;
};

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

1- Classes allow us to define member functions (methods) inside, structs are only for data
2- Methods are functions defined inside a class
3-
class Tree {
public:
string m_name;
void print(){
cout << “Name of the tree” << m_name;
}
};
4- An object is an instantiated class

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.
4. What is an object?
A data type that is defined by a given class.

1 Like

Why do we need classes, why can’t we just use structs?
- Because classes are greater. Classes include structs, though allow function in addition to simple data to be included.

What are methods?
- Methods, or member functions, are functions written within a class.

How would you define a class?

class ClassName
{
public:
//variables,
//functions, etc.
};

What is an object?
- An object is an element of a class. It can have many forms.

1. Classes provide much more power and flexibility. For example, classes can also contain functions and public or private variables.

2. Functions defined inside of a class.

3. class SampleClass { // define variables and/or methods here };

4. An instance of a class with allocated memory.

  1. Classes work to generalize and group data, and provide functionality, while structs just used to store related data. Also, classes can provide more complicated functions, like restricting access to variables and methods through the private keyword.
  2. Methods are functions accessible by a class instance.
  3. class MyClass {…};
  4. An object is an instance of a class.
Why do we need classes, why can’t we just use structs?

Classes differ to structs in that they are able to store functions that interact with the stored data within the object once instantiated.

What are methods?

A method or member function is a function that can be called from the class to interact with the data stored as noted above.

How would you define a class?

class Month {
public:
string name;
int numberOfDays;
};

Month january { “January”, 31};
cout << january.numberOfDays << endl;

The above code returns the number 31, revealing that the class has been correctly defined. Take note of the line of code that states public:, c++ defaults members to private and without a getter function the cout statement, outputting the number of days in january, will not work properly.

What is an object?

It is an instantiation of a class. Think of a class as a template (holding no values), and an object a version of the template that can hold data that we give it.

1 Like
  1. because strcuts can only hold data and not provide functions. classes can do that.
  2. methods are functions defined within the class
  3. it works exactly as a struct: class MyClassName {memberVariables, memberFunctions,…};
  4. an object is an instance of a class
  1. Why do we need classes, why can't we just use structs?
  2. Classes are like structs, that hold multiple data elements, but they also provide functions that work with the data.

  3. What are methods?
  4. Methods (or member functions) are functions evoked during a class. They can be declared inside or outside of the class definition.

  5. How would you define a class?
  6. Like structs, classes can be defined the same way, by using the keyword class instead of struct and the other exception of adding public:. For Example:

    class ClassName {
       public:
           //Data elements defined here
           int element1;
           string element2;   
    }
    

  7. What is an object?
  8. A variable of a type class is also called an object. When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.

    Source: Geeks for Geeks

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

  1. Here is an example:
    class ExampleClass
    {
    public:
    int var1;
    int var2;
    int var3;
    };

A variable of a class type is also called an object.

1- class alows us to provide functions, while struct only can store datas

2- Member functions ( or sometimes Method) are functions defined inside of a class

3- class nameClass{
public:
string name;
int …

};

4- a variable of a class, and a variable of a class is called an instance of a class

1- 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. In C++, this is typically done via the class keyword. Using the class keyword defines a new user-defined type called a class. In C++, classes are very much like data-only structs, except that classes provide much more power and flexibility.
2- Functions defined inside of a class are called member functions or methods.
3- first we put the keyword “class” and “ClassName” and data members and function members of the class would be placed inside the brackets :
class ClassName
{
// data members and function members
};

4- A variable of a class type is called an object or an instance of a class.

Quiz)
1-
#include

using namespace std;

class IntPair
{
public:
int m_int1;
int m_int2;

void set(int int1,int int2)
{
    m_int1=int1;
    m_int2=int2;
}

void print()
{
    cout<<"pair("<<m_int1<<","<<m_int2<<")"<<endl;
}

};

int main()
{
IntPair p1;
p1.set(1, 1); // set p1 values to (1, 1)

IntPair p2{ 2, 2 }; // initialize p2 values to (2, 2)

p1.print();
p2.print();

return 0;

}

2- Because we have to use member functions in order to set and print those variables ,so we define it as a class instead of a struct.

1. Why do we need classes, why can’t we just use structs?
Unlike structs, classes are able to hold functions as well as data values. This allows us to encapsulate related functionality within the class, making it better organized and also more resistant to errors caused by incorrect manipulation of the class data from outside the class.

2. What are methods?
Functions defined inside the class are called methods, or member functions.

3. How would you define a class?
A class can be defined using the “class” keyword. For example:

class DateClass
{
public:
    int m_year;
    int m_month;
    int m_day;
};

4. What is an object?
An object is an instance of a class. For example, DateClass could be a class and today is an object, instantiated in the following way: DateClass today { 2020, 10, 14 };

  1. Why do we need classes, why can’t we just use structs? classes are very much like data-only structs, except that classes provide much more power and flexibility
  2. What are methods? Functions inside the class
  3. How would you define a class?
    class DateClass
    {
    public:
    int m_year;
    int m_month;
    int m_day;
    };
  4. What is an object? nested atributtes and inicial data and can be
    inherited

1. Why do we need classes, why can’t we just use structs?
Classes holds member variables and member functions whereas structs suppose to only store variables.

2. What are methods?
Member functions to perform some tasks or operations.

3. How would you define a class?
Class represent a custom data structure which consists of logically related variables and methods.

4. What is an object?
Object is a way by which class can be used by instantiating it.