Classes in C++ - Reading Assignment

1.Classes are pretty much the same as Structs tho… Classes are types that not only hold data they provide functions that work within that data as well.

  1. Methods or otherwise knows as Member Functions are functions defined inside a class. They can be defined inside or outside the class definition.

  2. Class works the smae way as a Struct but you write Class.

class ClassName { //do some stuff here int element; string element2; }

  1. An Object is an abstract data type created by a dev. It can contain multiple properties and methods and can contain other Objects.

Object is an instance of a class or other types :slight_smile:

1 Like

Thanks mate. I am trying not to look at others answers and allowing myself to be corrected .

1 Like

@Alko89 thank you for the detailed explanation.
Notes taken.

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

Classes allows types to hold data as well as provide functions that work with the data. Whereas structs only holds data.

  1. What are methods?

Functions defined inside of a class

  1. How would you define a class?

Using the keyword “class”, followed by the name of the class

  1. What is an object?

An object is an instance of a class

1 Like

1.) Classes are able to contain functions as well as data, whereas structures only hold data.
2.) A function defined within a class is called a member function, or a method.
3.) class Stuffclass{ public: String food{}; String items{}; };
4.) An object is what building a class makes.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    – Both Structs and Classes define user data types; but additionally, Classes provide functions or methods that can work on and manipulate the data which is something that is often needed.

  2. What are methods?
    – Methods are function or member functions defined within a class

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

  4. What is an object?
    – An object is a definition of a variable of the Class type in question

1 Like

Classes can hold functions and data, which gives it more functionality and flexibility.

Functions within a class.

class ClassName
{
public:
int m_example;
};

An object is a variable of the class type.

1 Like
  1. We need classes so we can add function to the stored data. Structs only store data.

2.Functions defined inside a class.

  1. By using the keyword class followed by the name of the class

4.It is an instance of a Class.

1 Like
  1. With structs, we can only store data. With classes we can provide functions that works with data.
  2. Methods are member function of a class
  3. Class Students {
    public:
    Int number of student;
    Int age
    };
  4. An object is an instant of class
1 Like

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

We want an object that not only stores data, but also stores functions to work with that data

2. What are methods?

Functions contained within a class (also called member functions)

3. How would you define a class?

class Classname
{
public:
int value1;
int value 2;
};

4. What is an object?

a variable, function or other data structure contained within a class

1 Like
  1. In terms of language, except for one little detail, there is no difference between struct and class. Contrary to what younger developers or people coming from C believe at first, a struct can have constructors, methods (even virtual ones), public, private and protected members, use inheritance, be templated… just like a class.
    The only difference is if you don’t specify the visibility (public, private, or protected) of the members, they will be public in the struct and private in the class.
    Contrary to a struct, a class is made to offer an interface, that has some degree of separation from its implementation. A class is not just there to store data. In fact, a user of a class is not supposed to know what data the class is storing, or if it contains any data at all for that matter. All he cares about is its responsibilities, expressed via its interface.
  2. Functions.
  3. A class is a user-defined data type that we can use in our program, and it works as an object constructor or a “blueprint” for creating objects.
  4. An object is an instance of a class.
1 Like

:thinking: I had to check online, I guess you’re right. In C you can only have pointers to functions and using this method you can actually achieve a degree of Object programing in C.

2 Likes
  1. Why do we need classes, why can’t we just use structs?
    Classes allow us to provide functions with the data. Structs only store data but can’t provide the functions.

  2. What are methods?
    Functions defined inside of a class are called member functions

  3. How would you define a class?
    class Food{
    functions;
    objects;

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

1 Like
  1. In structs we can’t have functions as members while classes provide us with the option to include member functions in them and therefore organize our code in a logically better way (breathing is the action of adding oxygen to your body - function and this is something specific for the class “creature” => it doesn’t make sense to store it outside the object).

  2. Methods are function members - functions which are members of an object (class).

  3. The same way you define struct:
    class ClassName {
    memberType memberIdentifier;

    }

  4. In the particular topic scope - instances of classes. From a more philosophical perspective - everything can be treated as an object.

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

  2. functions that are defined in a class are called methods or member function.

  3. class Example {
    public:
    string m_name;
    void print(){
    cout << “Name of the example” << m_name;
    }
    };

  4. an instance of class

1 Like

1-We need classes, because structs can only store several data types into a single unit, but classes can also add functions into a single unit.
2-Methods are functions defined inside a class.
3- class className {


};
4-an object is an intance of class

1 Like
  1. Why do we need classes, why can’t we just use structs? Data-only structs (structs that only contain variables) represent the traditional non-object-oriented programming world, as they can only hold data. Classes provide functions that work with the data as well, not only hold it.
  2. What are methods? In addition to holding data, classes (and structs) can also contain functions. That functions, defined inside of a class are called methods.
  3. How would you define a class?
    class ClassName
    {
    //define variables,
    //define functions,
    };
  4. What is an object? 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?
    -> Because struct cannot hold functions in it.

  2. What are methods?
    -> It is a function declared inside the class.

  3. How would you define a class?
    -> class Classname{
    member variables
    member function
    }

  4. What is an object?
    -> It is a child of the class created.

1 Like

1. Why do we need classes, why can’t we just use structs?
Structs only hold data where classes hold the data as well as provide functions that work with the data

2. What are methods?
They are member functions defined inside a class

3. How would you define a class?
class ExampleClass{
public:

};

4. What is an object?
A variable that is defined

1 Like