Classes in C++ - Reading Assignment

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

  • Classes provides more flexibility than structs. Classes can hold functions as well as data.

What are methods?

  • Methods are functions defined inside of a class.

How would you define a class?

class DateClass
{
public:
int m_year;
int m_month;
int m_day;

void print()
{
std::cout << m_year;
}
};
What is an object?

  • A variable of a class type is called an object.
1 Like
  1. Why do we need classes, why can’t we just use structs?

    Classes can be used to hold data and provide functions that work with the data, whereas structs only hold data.

  2. What are methods?

    Methods are functions defined inside of a class.

  3. How would you define a class?

    class NameClass
    {
    public:
    int data1;
    int data2;
    int data3;
    };

  4. What is an object?

    A variable of the class type is an object.

Quiz: Part 1:

#include
using namespace std;

class IntPair {
public:
int number1;
int number2;
void set(int num1, int num2)
{
number1 =num1;
number2 =num2;
}
void print()
{
std::cout<<“Pair(”<<number1<<", “<<number2<<”)"<<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;

}

Part 2:

Both data and functions are included. Structs are for data only.

1 Like

1. Why do we need classes, why can’t we just use structs?
We need classes because they allow us to solve more complex problems than regular structs. Classes let us define our own data types. With classes, it’s possible to have objects that besides data include functions (called member functions).

2. What are methods?
Methods is another term for “member functions.”

3. How would you define a class?
By using the class keyword…
class ExampleClass

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

1 Like
  1. A class is a way of creating custom data type that can contain one or more data type and other type such as function etc. It is much more flexible than the struct type. A class allows the creation of objects.
  2. They are member functions in a class
  3. With the key word class
  4. An instance of a class
1 Like

Why do we need classes, why can’t we just use structs?
We often want our types to not only hold data, but provide functions that work with the data as well, and this is typically done via the class keyword in C++.

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

How would you define a class
A class in C++ is a user defined type or data structure declared with keyword class that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. (wikipedia)

what is an object
An object can be referred to as a variable of a class type

1 Like

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

In the world of object-oriented programming, we often want our types to not only hold data, but provide functions that work with the data as well. In C++, this is typically done via the class keyword. Using the class keyword defines a new user-defined type called a class.

classes are very much like data-only structs, except that classes provide much more power and flexibility.

2. What are methods?

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

3. How would you define a class?

  1. class MyClass {…};

4. What is an object?

An object is an instance of a class

1 Like
  1. Why do we need classes, why can’t we just use structs?
    • structs can’t have functions as a member, classes can.
  2. What are methods?
    • functions defined inside a class
  3. How would you define a class?
    class car
    {
    public:
    string make;
    string model;
    int year;
    string color;
    }
  4. What is an object?
    • An object is an instance of a class
1 Like
  1. We need a class because a struct cannot hold functions so for storing functions also in a user defined data type we need classes.
  2. Methods are the functions included in a class.
  3. class nameOfClass {
    //content
    }
  4. An instant of the class is known as object.
1 Like
  1. Classes allow us to store data and provide functions that work with the data, while struct only allows us to store data.
  2. Methods are the functions defined inside or outside a class.
  3. I would define a class as:
    class theDate {

    }
  4. An instance of class
1 Like

1. Why do we need classes, why can’t we just use structs?
Classes provide much more power and flexibility. They provide functions that work with the data as well.

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

3. How would you define a class?

class DateClass
{
public:
int m_year;
int m_month;
int m_day;

void print()
{
std::cout << m_year << “/” << m_month << “/” << m_day;
{
};
4. What is an object?
A variable of a class type

1 Like

1. Why do we need classes, why can’t we just use structs?
Use the class keyword for objects that have both data and functions, and the struct keyword for data-only structures.
2. What are methods?
Functions defined inside of a class are called member functions (or sometimes methods )
3. How would you define a class?
The class keyword defines a new user-defined type called a class.

4. What is an object?
A variable of a class type is also called an object

1 Like

Hello @ivan and dear Community,


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

Structs contain only data. Thus, it’s not able to perform operations on the variable that is kind of user-defined datatype made out of a struct. But classes can do!

  1. What are methods?

Methods are functions inside a class, also called “members of a class”.

  1. How would you define a class?

class MyClass() {private: //TODO};

  1. What is an object?

A user-defined data type that is made out of a class and was instanced.


Cheers
Kevin

  1. Why do we need classes, why can’t we just use structs?
    Classes provide functionality and data whereas structs only have data.
  2. What are methods?
    Functions define in class
  3. How would you define a class?
    class myClass {
    }
  4. What is an object?
    Instance of a class
  1. Why do we need classes, why can’t we just use structs? Classes allow us to create user-defined types of variables or data. This is useful in object-oriented programming.
  2. What are methods? These are functions defined inside a class. They’re also called member functions.
  3. How would you define a class? Use the word class followed by the name of class and then enclose in braces the declarations. At the top prior to declarations, type the keyword public: then define the variables as you do in struct. Don’t forget semicolon as closing of each class.
  4. What is an object? It is a variable of a user-defined class type.
  1. Why do we need classes, why can’t we just use structs?
  • Because structs are limited to store data and you cannot have functions as members. With classes, we can store functions that work with data.
  1. What are methods?
  • methods are functions defined within classes.
  1. How would you define a class?
  • class newClass{
    public: // public members and methods
    private: // private members and methods
    }
  1. What is an object?
  • A variable of type class
  1. Why do we need classes, why can’t we just use structs?
    We need classes to define member functions (methods) inside a self created data type

  2. What are methods?
    Methods (member functions) are functions written within a class.

  3. How would you define a class?
    class{
    functions;
    objects;
    //access to other classes;
    };

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

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

Because structs can only handla members type that is data while classes can also handle member functions.

What are methods?

Member functions.

How would you define a class?

Class randomname{

Public:

//public members and methods

Private:

//private members and methods

};

What is an object?

A variable of a class.

1. Why do we need classes, why can’t we just use structs?
Structs are a data type that are used to hold only data, while classes can hold data and functions that operate with that data.

2. What are methods?
They are the functions inside a class.

3. How would you define a class?
class myClass{
public:
int A;
string B;
double C;

void myFunction(){
cout << A << B << C;
}
};

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

Why do we need classes, why can’t we just use structs?
Structs only contain data, Classes can contain functions

What are methods?
Functions within Classes

How would you define a class?
With a capital letter

What is an object?
An Object is a single instance of a Class

1. Why do we need classes, why can’t we just use structs?
structs cant have functions inside of them, class can provide more flexibility in some cases since they can have inside functions, public/private variables, etc…
2. What are methods?
Functions defined inside the class.
3. How would you define a class?
class SmashThatLikeButton{
//variables/functions code here
};
4. What is an object?
Instance of a class, variables members…

1 Like