Classes in C++ - Reading Assignment

1. Why do we need classes, why can’t we just use structs? Because we can’t include functions in struts.

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 myClass {
       public:
       //variables,
       //functions, etc.
};

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

2 Likes

1)Why do we need classes, why can’t we just use structs?
In order to create data types that have built-in functions to interact with their data. With classes, data structures do not need to be passed to a separate function.

2)What are methods?
Functions defined inside of a class

3)How would you define a class?
Much like a struct, but with the class keyword, public types, and methods defined within the object.

4)What is an object?
An object is data structure that contains data and functions (methods) that act on that data. In other words, it is an instantiation of a class

1 Like
  1. Classes allow us to use functions with data, structs only store data
  2. Methods are functions that can be defined inside or outside of the class definition
  3. class NameOfClass {
    public:
    variable
    method
    };
  4. Objects are instances of a class
1 Like
1. Why do we need classes, why can’t we just use structs?

Classes are meant to be fully functional user defined types. They include both variables and functions. By convention, structs are for data only.

2. What are methods?

Class members that are functions.

3. How would you define a class?

With the keyword class

4. What is an object?

Object is a generic term. Both classes and structs are objects. Instances of classes or structs are objects. Technically speaking all classes inherit from Object, which is the base class for all objects and provides the functionality common to all objects (like ToString()).

Quiz

1) IntPair class

Note: it’s a better practice to make the member variables private instead of public. You then use get and set functions to access the values. This way you have complete control over how these values are updated and acessed.

#include <iostream>

using namespace std;

class IntPair {

public:
    int m_a;
    int m_b;

    void set(int a, int b) {
        m_a = a;
        m_b = b;
    }

    void print() {
        cout << "Pair(" << m_a << ", " << m_b << ")\n";
    }
};

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

Why do we need classes, why can’t we just use structs?
With structs we can store only data. But with classes we can provide functions to the data aswell.
What are methods?
Functions defined inside of a class.
How would you define a class?
class ClassName{


};
What is an object?
An instance of a class

1 Like
  1. Structs = data only; Classes = data & functions
  2. Functions in the classes
  3. class XXX {
    // functions/methods
    // objects
    }
  4. It is a variable

4. What is an object?

Hello sir, that is not the accurate answer, an Object is simply a collection of data and functions that act on those data.

Hope this gives you a clear view of the subject, keep learning! :slight_smile:

If you have any doubt, please let us know so we can help you!

Carlos Z.

  1. Classes hold data like structs do, but they also are able to execute functions.

  2. Functions within classes or ‘member functions.’

  3. They are like structs in that they hold multiple pieces of data for one variable but also include functions.

  4. An object is a descriptor in a class.

1 Like

1. Why do we need classes, why can’t we just use structs?
Structs contains only data but classes are using in addition functions which work with data.
2. What are methods?
Methods are functions defined in classes.
3. How would you define a class?
class Employee {
public:
string name;
int age;
double wage;
};
4. What is an object?
To create an object a variable of a class must be defined.
E.g. Employee john {John, 37, 1800}; // declare a variable of class Employee

1 Like

Answer:

    1. Classes are useful if you want to store functions inside of your objects. Structs can’t hold functions. They can only store data.
    1. Methods are functions defined inside of the class. Sometimes they are called member functions.
    1. class NameOfTheClass
      { public/private:
      type memberName;
      };

      Classes defined similar to structs.
    1. An object is a variable that can store data and functions. Classes by themselves are not objects, they are blueprint for a potential object. To create an object, variable of that class type has to be defined.
1 Like
  1. Why do we need classes, why can’t we just use structs?
    Structs can only hold data, classes add another layer of functionality that allows us to work with the data inside our classes.

  2. What are methods?
    Functions that allow us to perform actions on the class (once instantiated).

  3. How would you define a class?
    Using the class keyword.

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

1 Like
  1. We use classes because we sometimes want something that can not only hold data but also functions.

  2. Methods are functions that are found within classes. They can also be called member functions.

  3. A class is a user defined data type which holds data members and methods.

  4. An object is a type of data that is defined by a given class

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Classes can encapsulate functionality by providing functions that operate on the data.

  2. What are methods?
    The functions associated with a class.

  3. How would you define a class?
    Similar to a struct.
    class MyClass {…};

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

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Classes will provide functions that interact with data

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

  3. How would you define a class?
    class yourClassName { //add variables and methods };

  4. What is an object?
    an instantiated class

1 Like

Classes homework.

1 - Why do we need classes, why can’t we just use structs?
Both structs and classes, technically, can hold data as well as functions. It is considered good practice to use Classes for instances where functions are required as well as data.

2 - What are methods?
Functions stored inside of classes are called methods.

3 - How would you define a class?
class Cars
{
public:
string m_manufacturer;
int m_age;
int m_milesDriven;
};

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

Quiz solution:

#include <iostream>
using namespace std;

class IntPair{
public:
    int var1;
    int var2;

    void set(int x,int y){
        var1 = x;
        var2 = y;
    }
    void print(){
        cout << "Pair(" << var1 << ", " << 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;
}
2 Likes

1. Why do we need classes, why can’t we just use structs?
structs can create custom datatypes. However if we need to perform functions on struct data, classes have to be used
2. What are methods?
Methods are member functions associated with a class
3. How would you define a class?
class SampleCLass {
// declare member variables
// declare member functions / methods
};
4. What is an object?
Is an instance of class. A class itself does not store memory but an instance of a class can allocate memory.

1 Like
  1. Classes can have member functions
    and are more flexible when it comes to combining data and functions. Structs is more used
    to group data

  2. Methods are functions defined inside a class or structs.

  3. A program code that gathers data and functions to the named object in the class

  4. The actual name of the class, which preferrably should be named
    related to the info and functions related to the class.
    You can also say it is a collection of data

1 Like
  1. Structs do not allow functions to be included. With classes, data-related functions can be integrated into a new data type.
  2. Methods are functions defined inside of a class.
  3. class sampleClass {public: int a; string b; void printText (){code}};
  4. An object is a defined element of the data type class.
1 Like
  1. Unlike in structs, classes can provide functions that work with the data as well.
  2. Methods are defined functions within classes.
  3. A data structure in which we can create objects. It’s crucial in object oriented programming.
class SampleClass {
// define variables and/or methods here
};
  1. A data type defined by a specific class with allocated memory.
1 Like
  1. Classes is different to structs in a way where they are able to store functions that interact with stored data within the object.
  2. Methods are functions within a class.
  3. A class is a structure of data that enables the developer to create objects.
  4. An object is a data type that is defined by the class.
1 Like