Classes in C++ - Reading Assignment

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

Structs and classes alike can have both member variables and functions in C++. However, it cannot be assumed that a struct will clean up after itself (deallocating memory when no longer needed) in the same way as a class. Therefore a class should be used when member functions are required and structs only for member variables .

What are methods?

Methods is another word for member functions within a class.

How would you define a class?

class ClassName
{
public:
int m_variable1;
string m_variable2;

void method1()
{
}

};

What is an object?

An object is an instantiation of a class. It is the data structure that gets created by the compiler to the specification of its defined class. Multiple objects may be created from the same class with their own set of values.

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?
className
{
public
}

4. What is an object?
A data type that is defined by a given class.

1 Like

You mean members of a class? Since the objects that are defined outside are visible in the entire scope.

1 Like

yes indeed I needed to specify , thanks for the remark!

  1. Use the struct keyword for data-only structures. Use the class keyword for objects that have both data and functions.
  2. Methods are functions declared in a class.
  3. class MyClass{
    /*MyClass description
    */
    };
  4. An object is an instance of a class
1 Like

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

Classes allow us to do something with data, whereas structs do not (well they do, but generally we use structs for holding data and classes for doing something with its own data.

2. What are methods?

The member functions.

3. How would you define a class?

class Tester
{
print:
int m_member1;
int m_member2;
string m_member3;

   void tester () {
           ...code...
   } 
   void tester () {
           ...code...
   } 

};

4. What is an object?

An object is the initialised “variable” of a data type. All the rules defined within the data type (variables or functions for example) apply to it. In classes, the values derived from the object’s members, serve as information for its member functions.

1 Like

Think about the following questions while reading:

  1. Why do we need classes, why can’t we just use structs?
    Structs can only contain data types, but not methods. If you need a combination of member variables and member functions you will need to use a class.

  2. What are methods?
    Methods are member functions within a class, that are able to access or manipulate objects.

  3. How would you define a class?
    A class starts with the keyword “class” and should be named with a capital letter at the beginning of its name. A class can contain several member variables of different types and member functions as well.

  4. What is an object?
    An object refers to a particular instance of a class, where the object can be a combination of variables, functions, and data structures.

1 Like

1. Why do we need classes, why can’t we just use structs?
A class containing member functions will clean up after itself. it deallocates memory.
It’s not safe to assume a struct will do this.
Therefore structs are used for data only structures

2. What are methods?
Member functions

3. How would you define a class?
It’s a type or data structure with variables and functions that can be declared with ‘class’

4. What is an object?
a class can be a programming object. Objects can contain data and code.
And if you call a class function. you call the function on an implicit object.

1 Like
  1. Structs are data only structures. when we use the class keyword it helps provide a way of bundling defining objects that require both data and functions together. Classes allow us more power and flexibility.

  2. classes can hold both data and functions. functions inside of a class are called member functions, or sometimes methods.

  3. I would define a class as similar to a struct, except with a class having data and functions also called member variables and member functions. these members whose access is governed by three access specifiers. Private, Protected, Or Public. Private members are not accessible outside the class, but can be accessed only through methods of the class. Public members form an interface to the class, and are accessible outside the class.

4.an Object is a variable of a class type that must be defined.

1 Like

Why do we need classes, why can’t we just use structs?
The class keyword lets us create a custom type in C++ that can contain both member variables and member functions.
(structs that only contain variables)

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

How would you define a class?
void print() // defines a member function named print()
{
std::cout << m_year << ‘/’ << m_month << ‘/’ << m_day;
}

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. Defining Class and Declaring Objects. A class is defined in C++ using keyword class followed by the name of class.

1 Like

This is correct, but how do you define a class with this function? :wink:
Btw you can use a block code to make the code more readable, use three apostrophes before and after the code block, like this:

// ``` <-- before code block

void print()
{
std::cout << m_year << ‘/’ << m_month << ‘/’ << m_day;
}

// ``` <-- after code block
2 Likes

Thanks :blush:

We need classes in addition to structs because structs only hold data whereas a class can hold data and member functions.

Methods are a synonym for member functions, which are functions within the scope of a class.

I would define a class as a specific type of object that can contain member functions and variables.

I would define (instantiate) a class within code by doing something like the following example:

Class Employee

{

public:

string m_name {};

double m_wage {};

};

An object is a set of data { }; further specified, it can be either a struct or a class, depending on contents.

1 Like

Reading Assignment: Classes in C++

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

Classes is struct on steroids, and classes can also contain functions and public or private variables.

2. What are methods?

A function inside a class

3. How would you define a class?

Class my_class

{

public:

int year{};

};

  1. What is an object?

Object is a member in the class.

1 Like

Why do we need classes, why can’t we just use structs?
A class gives us the ability to hold data, and provides functions that help us to work with the data as well.

What are methods?

Methods, also known as member functions.

How would you define a class?

They are defined inside or outside the class definition.

What is an object?
Object is the information or data being stored, or manipulated in a struct or a class.

#include <iostream>
 
class IntPair
{
public:
    int m_a{};
    int m_b{};
    
    void set(int first, int second)
    {
        m_a=first;
        m_b = second;
    }
    void print()
    {
        std::cout << "pair(" << m_a<< "," << m_b<< ")\n";
    }
};
int main()
{
    IntPair p1;
    p1.set(1, 1);
    
    IntPair p2{2, 2};
    
    p1.print();
    p2.print();
    
    return 0;
}

This class contains both functions and member data. It is more complex, and should be a class. Structs are for more simple operations.

1 Like
  1. structs are used to contain only data and not functions, as opposed to classes.

  2. methods are functions defined inside of a class.

  3. for example

class Animal {

private:
      string m_animalName;
      int m_legsNumber;

public:
      
      void printName() {
           cout << "This animal's name is " << m_animalName << " \n";
      }
}
  1. An object is an instance of a class
1 Like

You forgot to write a constructor to initialize the variables :wink:

1 Like
  1. We need classes because we often want our types to not only hold data, but provide functions that work with the data as well. Functions can be defined also in structs, but programming convention says structs should be used only for data-structures.
  2. Methods or member functions are the functions defined inside a class.
class DateClass
{
public:
    int m_year{};
    int m_month{};
    int m_day{};
 
    void print()
    {
        std::cout << m_year << '/' << m_month << '/' << m_day;
    }
};
  1. An object is one instance of the class.

Quiz:

class IntPair {
    public:
        int m_var1 {};
        int m_var2 {};

        void set(int v1, int v2) {
            m_var1 = v1;
            m_var2 = v2;
        }

        void print() {
            cout << "Pair(" << m_var1 << ", " << m_var2 << ")" << 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;
}
1 Like

Your quiz solution will not work (maybe it does on some compilers). You could achieve this in two ways, either by defining constructors that accept vars as a parameter or to overload the = operator.
Mind if you define a constructor you would also have to define a default constructor that doesn’t accept parameters or your first example would fail. :slight_smile:

This is how I would extend your solution to make it work either way:

class IntPair {
    public:
        int m_var1 {};
        int m_var2 {};
        
        IntPair(){} // Define a default constructor
        IntPair(int v1, int v2) { // A constructor that initializes vars
            set(v1, v2);
        }
        
        void operator = (int v[2]) { // Overload an operator to accept int array and set vars
             set(v[0], v[1]);
          }

        void set(int v1, int v2) {
            m_var1 = v1;
            m_var2 = v2;
        }

        void print() {
            cout << "Pair(" << m_var1 << ", " << m_var2 << ")" << endl;
        }
};


int main()
{
	IntPair p1; // initializes IntPair using the default constructor
	p1.set(1, 1); // set p1 values to (1, 1)

	IntPair p2{ 2, 2 }; // initialize p2 values to (2, 2) using the operator
	
	IntPair p3( 3, 3 ); // initializes p3 values to (3, 3) using the constructor

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

	return 0;
}
3 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 work with data. Methods are the added-value of classes. We can refer as a class for inheritance. I don’t need to repeat all a human can do and add a profession and gender by example. The struct don’t permit this flexibility.

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 or a primitive variable that caries it owned methods like String

1 Like