Classes in C++ - Reading Assignment

  1. Why do we need classes, why can’t we just use structs?
  • Classes hold data but also provide functions that work with the data too. Classes and structs are essentially the same, but classes are public
  1. What are methods?
  • Methods are also known as member functions. Methods are functions defined inside of a class for simplicity; however, they can also be define outside of it’s class too
  1. How would you define a class?
  • class Class{

public;

//your variables

};

  1. What is an object?
  • Objects are instances of class. They hold the data variables declared in the class.
1 Like

Why do we need classes, why can’t we just use structs?
Classes give us the ability to also add functions to a collection of data

What are methods?
These are the functions which are embedded with a class.

How would you define a class?
class GoalsScored
{
public:
int m_Home;
int m_Away;
int m_CupMatch;
};

What is an object?
It is a variable of type Class. So it can have multiple data types and embedded functions.

1 Like

Answers

  1. We need classes for objects that have both data and functions.

  2. Methods are member functions inside of a class.

class ClassName {
   public:
   member 1;
   member 2;
   methods 1;
   };
  1. An object is an instance of a class with allocated memory.
1 Like
  1. With structs, we can only store data. In addition to holding data, classes can also contain functions

  2. Functions within a class are called member functions or methods

  3. class MyClass {
    ...
    };

  4. A class provides the blueprints for objects, so basically an object is created from a class. We declare
    objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box −
    Box Box1; // Declare Box1 of type Box
    Box Box2; // Declare Box2 of type Box
    Both of the objects Box1 and Box2 will have their own copy of data members.

1 Like
  1. We use the struct keyword for data-only structures. We use the class keyword for objects that have both data and functions.
  2. Functions defined inside of a class.
  3. Using the ‘class’ keyword.
  4. An object is an instance of a class. A class is like a blueprint for creating objects.
2 Likes
  1. Because classes allow us to use not only data, but also functions within their structure. They include what we call member functions.

  2. Methods (or member functions) are functions defined inside of a class.

  3. A class is a user-defined data type that works as a blueprint for creating objects.
    We define it by using the class keyword, such as in the example below:

class MyClass
{
public: // We will learn later the meaning of the "public" keyword
int m_variable1;
int m_variable2;
int m_variable3;
};
  1. An object is an instance of a class. When a class is defined, no memory is allocated but when an object is created, memory is allocated.
1 Like

I have two related questions in regards to this exercise:

How can we compare classes in C++ with classes in other programming languages such as Java, JavaScript or Python?

Is C++ a fully object-oriented language?

1 Like

Hi, I’ll try to answer it.

I think of classes in C++ as the same as I do for classes in Python.

I might be wrong on this answer but I’ll try:
Object-oriented programming is when you build and structure your program around objects and classes.
function-oriented programming is when you build and structure your program around functions.

I was a little unsure so I just searched “OPP” in google, and this was the first result i got.

fist section: Significant object-oriented languages include Java, C++, C#

i hope this helps.

  1. We need Classes because it helps to simplify and organize variables with same properties and with same Functions needed.

  2. Methods are members functions or functions defined inside of a class.

  3. A class would be defined exactly as we doing a Struct except a feature which could be either public or private:

Class class{
public: //
string classSubject;
int numberOfStudents;
int numberOfHours;

int functionMethod ( classSubject, numberOfStudents){
cout<<“bla bla bla”;
}
};

  1. an obejct is a defined class with its defined var&function
1 Like
  1. Why do we need classes, why can’t we just use structs?
    Classes and structs are essentially the same. The only difference between a class and a struct are default members. Structs have default public members and classes have default private members.
  2. What are methods?
    Methods or member functions are functions defined inside of a class. They can be also defined outside of the class definition…
  3. How would you define a class?
    Class is defined by using the class keyword.
  4. What is an object?
    An Object is an instance of a class which has memory allocated to it.
1 Like

Indeed sir, also a class can provide more flexibility in some cases since they can have functions inside of them, public/private variables, etc…

While structs cannot have any function inside them, only members has you said.

Carlos Z.

  1. Why do we need classes, why can’t we just use structs?
    Struct can hold variables of different types. Class can also hold functions.

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

  3. How would you define a class?
    Similar to a struct, but with the statement “public” and with functions.

class className{
public:
int variable1;
int variable2;
int variable3;

void functionName(){
    std::cout << "I'm a function" << endl;
}

};

  1. What is an object?
    A named variable of a class type associated to a memory address.
1 Like
  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
  1. Classes can contain multiple data from multiple data types as well as member functions to manipulate/ operate with the data inside. With structs you can only store multiple data types but you cannot write related specific functions to this inside the struct. When you want to manipulate the data in the struct you have to write specific functions outside of the struct. In this case, to make the code more clean and aggregate different functionalities together you should use classes.

  2. When you define a function inside (but also outside!) a class it is called a method or member function. That means it is a specific method for this class.

  3. You can see a class as a basic framework from something which contains and consequently combines data representation as well as suitable methods for manipulating this related data in one group/package. Classes are way to abstract things out of the reality to a lower level and therefore to make it describable and manipulable

  4. When you create a variable which is from the data type of your class, then you create an object. Objects are created out of a class. So, when you bring something from a class to life (means assign values to it), it is an object.

1 Like
  1. Classes are useful because they can contain functions, however structs do too in C++ (so you could use both, it is just a matter of preference).
  2. Methods (or member functions) are functions within a class.
  3. By using the “class” reference when creating it and the “public:” keyword.
  4. An Object is an instance of a class.
1 Like

Why do we need classes, why can’t we just use structs?
The only significant difference is the public: keyword in the class.

What are methods?
ts a function inside a class and can slide be called a member.

How would you define a class?

class myClassExsample {

Create functions or methods in here both public and private*

};

What is an object?
An object is an instant of a class with memory

1 Like

1. Why do we need classes, why can’t we just use structs?
Classes are used to store data types and functions whereas structs are used to only store data types.

2. What are methods?
These are functions within a class.

3. How would you define a class?
class ExampleClass{
public:
int m_anInteger;
string m_aString;
void print() {
cout << aString << anInteger << endl;
}
};

4. What is an object?
An object is an instance of a class that has been created.

1 Like
  1. Classes allow us to store data and functions, while structs don’t allow us to store functions.

  2. Methods are functions defined inside or outside of the class definition.

  3. class myClass { public: int variable1; int variable2; }

  4. An object is an instance of a class.

1 Like
  1. Classes allow us to not only hold data, but to provide member functions as well.
  2. Functions defined inside the class are called methods.
class Newone {
   public:
   ....
}
  1. An Object is an instance of a Class.
1 Like
  1. Often we want our types to not only hold data, but to provide functions that work with the data as well.

  2. Functions defined inside of an class.

  3. Class thisClass
    {
    public:
    int x
    int y
    int z
    };

  4. Instance of a class with allotted memory.

1 Like