Classes in C++ - Reading Assignment

1. Why do we need classes, why can’t we just use structs?
Classes are important as they allow us to incorporate functions as well as data.

2. What are methods?
Methods are another name for member functions (functions within a class).

3. How would you define a class?
To define a class you use the keyword class followed by a definition name, ideally starting with a capital letter to differentiate it from a standard variable type, then you define each of the members within curly braces. This does not instantiate an object, it merely defines what it will look like.
This is what a class might look like:

class Maths
{
    public:
        int firstNumber;
        int secondNumber;
        void printResult()
        {
            cout<<"When added together, your numbers ("<<firstNumber<<" + "<<secondNumber<<") equal = "<<firstNumber+secondNumber<<endl;
        };
};

int main()
{
    Maths sum_1{8, 4};

    sum_1.printResult();


    return 0;
}

4. What is an object?
An object is just about anything that has been instantiated. A struct, array, function, class , etc. are all objects, but only once they have had a variable or function name assigned to them - at this point the computer creates and assigns memory to the object.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    More complex problems become difficult to solve with only the fundamental data types (e.g. char, int long, float, double, etc…).
    Allows for specified user-defined ‘types’ that are more effective for new conception.
    Structs can only hold data, classes allow us to hold data AND use functions that work with the data.
    This is Object Oriented programming.

  2. What are methods?
    Also called Member Functions.
    These can be defined in or out of the class definition.
    Access a method with the member selector operator (.)
    Must be associated with an object of the class.
    Ex. “today.print()” tells the compiler to call the print() method [or member function] associated with the today object.

  3. How would you define a class?
    Class (and struct) definitions are like a blueprint, describing what the resulting object will look like, but they do not actually create the object. To create an object of the class, a variable of that class type must be defined.

  4. What is an object?
    This is an instance (or variable) of a class type.
    Objects bundle both data and functions together.

1 Like

Member functions are always defined inside a class definition. A function defined outside of a class is…well…just a function :slight_smile:
Though this just terminology, I’ll let you pass :smiley:

1 Like

1. Why do we need classes, why can’t we just use structs?
With structs only data should be stored. Classes where introduced to also contain member functions. These functions work with the data of the called object.

2. What are methods?
Methods are functions which are executed upon the data belonging to the class objects instance.

3. How would you define a class?
A class is an object which holds multiple variables for data and functions which are applied to the data of these.
class TheClass {
public:
int m_var1;
double m_var2;

function(){
}
};

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

1 Like
  1. Why do we need classes, why can’t we just use structs? Classes provide a way for type to not only hold data, but to provide functions that work with the data.
  2. What are methods? Functions defined inside a class are called member functions or methods.
  3. How would you define a class? Use the public keyword.

Class Person

{

public:

string name;

int weight;

int height;

};

  1. What is an object? An object is an instance of a class. For example, Tom can be an instance of the class person. Tom is the object that we are working with.
1 Like

class keyword is used to define a class :slight_smile: public keyword defines the visibility scope.

1 Like

Objet Oriented Programming (OOP); Classes & class members.

  1. Why do we need classes, why can’t we just use structs?
    Using the class keyword lets you create a custome type that can contain both member variables and member functions. A struct function will only allow you to hold data.

  2. What are methods?
    Functions defined inside a class(keyword) are called member functions, also known
    as methods.

  3. How would you define a class?
    The keyword that is used after the class function, defines the class. Using the class keyword defines user defined class.

  4. What is an object?
    a variable with data that declares the class type user defined keyword.

1 Like

1.Because in classes we don’t only have data but also functions.
2. Methods are functions defined in the class.
3.class ClassName {
public:
//Data elements defined here
}
4. A variable of a class type is also called an object.

1 Like

1. Why do we need classes, why can’t we just use structs?
The default accessibility of member variables and methods. In a struct they are public; in a class they are private
2. What are methods?
Functions defined in a class
3. How would you define a class?
M-prefix
4. What is an object?
Its attached to the method and its what is passed to the function

Not true, the reason there are classes in C++ is so we can define methods that can manipulate variables inside a class. Visibility scope in a class is basically just an added protection so a developer can’t make some dumb mistakes when writing code.
True you don’t really need classes if you want to just have everything public and define a struct with variables that are pointers to functions outside a struct. In fact you can do this in C, which does not have classes to achieve a basic object oriented paradigm in a language that doesn’t support objective programming. :slight_smile:

/***  FUNCTIONS ARE DEFINED HERE  ***/

typedef struct tsip_stack_s {
    TSK_DECLARE_OBJECT; // A global object every struct inherits...inheritance in C!!!
    tsk_bool_t started; // Is the stack running
}
tsip_stack_t;

tsip_stack_def_s = {
    sizeof(tsip_stack_t),
    tsip_stack_ctor, // A pointer to a constructor a sip_stack object
    tsip_stack_dtor, // A pointer to a destructor a sip_stack object
};
const tsk_object_def_t *tsip_stack_def_t = &tsip_stack_def_s;

Class is defined with a class keyword :slight_smile:

Object is basically an instance of a class.

1 Like

Looks like I need to reread the chapter :triumph:

1 Like

Sorry :sweat_smile: I may have overcomplicated stuff a bit for you. Sometimes people already know that stuff and are just trolling…this answer was intended for these kinds of people :stuck_out_tongue:
You don’t need to read the part about how to achieve class like structures in C (which does not have classes, but does contain structs).
You just have to know how to create a class in C++ and why you would prefer to use one over the other :slight_smile:

2 Likes

No trolling here, just an old dinosaur who has no idea what he’s doing lol. Thanks for feedback

  1. Why do we need classes, why can’t we just use structs?
    Classes have largely the same capabilities as structs. We use structs for data-only objects and classes for data and methods objects.

  2. What are methods?
    Methods are functions within classes and structs.

  3. How would you define a class?
    Same way as defining a Struct object but using the Class keyword.

  4. What is an object?
    It is a data type that can contain other data type or methods. For example, a class would be an object.

1 Like

An instance of the class is an object, not the definition of the class itself :slight_smile:

3 Likes
  1. Classes allow the programmer combine functions ( methods) to work with data. Structures can only hold data.
  2. Methods (or member functions) are functions defined inside of a class.
class MyClass{
          public:
             int a; 
             int b;

        void functionName {
        }             
     };
  1. An Object is an instance of a class.
1 Like

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

Because classes can also contain functions in addition to just holding data, which is what structs mainly do.

2. What are methods?

Methods, also called member functions are functions contained within a class.

3. How would you define a class?

A class is a data structure which allows us to create instances of different objects by simply providing its properties and functions.

4. What is an object?

An instantiation of a class.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Classes allow us to create a new type. This new type can hold variable members and member functions.

  2. What are methods?
    Methods are another name for member functions. Member functions are held within the class and work with data within that class.

  3. How would you define a class?
    A class is a user defined type that holds data, object variables and functions within the class. The class is defined outside the main(). It is there class name is defined as well as the objects within and any functions that would work with those variables. It is not until a value is given to an object that it is instantiated. this occurs with main().

  4. What is an object?
    An object is a variable created in a type.

1 Like
  • Structs cant contain functions as members, classes are more versatile
  • funcions in class
  • class Person {
    string name:
    }
  • A variable of class type is called object
1 Like
  1. Why do we need classes, why can’t we just use structs? We may want our struct types in C++ and in the world of object-oriented programming, to not only hold data, but to have a function that works with the data as well. This is usually done with a class keyword.

2. What are methods?

“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.

  1. How would you define a class?”

Class (and struct) definitions are like a blueprint, they describe what the resulting object will look like, but they do not actually create the object. To actually create an object of the class, a variable of that class type must be defined.” “ Classes form the basis for object oriented programming. The C++ standard library is full of classes that have been created such as std :: string , std :: vector , and std :: array which are all class types .”

4. “What is an Object?”

“An object is call data passed to a function as per the programmers instruction. The associated object is then passed to the member function and often called the “implicit object”. When you create an object of any of these class types, you’re instantiating a class object . And when you call invoke a function using these objects , you’re calling a member function.”

1 Like