Classes in C++ - Reading Assignment

  1. Why do we need classes, why can’t we just use structs?***classes allow for both data and functions. structs allow for data only.
    2. What are methods? methods are member functions
    3. How would you define a class? with “class” followed by “public” and then the members by type
    4. What is an object? an object is the contents of a class or the body of a class
1 Like
  • Classes allow us to group functions and data together where structs do not.
  • Methods are functions within a class
  • class ClassName {
    public:
    // public members and methods
    private:
    // private members and methods
    }
  • An instance of a class
1 Like
  1. Structs only hold data, but we also want to provide functions in the object. Classes can hold data and functions (method).

  2. Methods are the functions defined inside of a class.

  3. Example:

    class Person {
      public:
      string name;
      int age;

      void print(){
        cout << name << " is " << " years old." << endl;
      }
    };
  1. An object is an instance of a class or struct.
1 Like
  1. Structs can store data but not provide functions. However, classes can do both.
  2. Methods are functions defined inside of a class. They are also known as member functions.
  3. Classes are user-defined types which simultaneously hold data and provide the functions which work with the data being held.
  4. Objects are instances of a class.
1 Like

1.Why do we need classes, why can’t we just use structs? We need classes because structs can only store data, and we use classes for objects that have data and functions.
2. What are methods? methods are functions defined inside a class or struct, sometimes called members.
3. How would you define a class? A class is defined by the member selector operator. class name { public: …};
4. What is an object? An object is an instantiated class.

1 Like
  1. We often want our types to not only hold data but provide functions that work with the data as well.
  2. Methods are functions defined in classes.
class myClass {
public:
    int varA{};
    int varB{};
    int varC{};
private:
    int varD{};
    int varE{};
    int varF{};
};
  1. An object is an instance of a class.
1 Like
  1. Because structs cannot use functions, only store data.

  2. Methods are functions which are a member of a class.

  3. By using the class keyword ie class DateClass.

  4. An object is something which stores pieces of data.

1 Like

To solve simple problems, we ca use structs but if we want to solve complex ones, like functions work with the data, we need >>classes<<.

Methods are functions defined inside of a class.

the same structure as we form Structs but with the word “public”
class DateClass
{
public:
int m_year;
int m_month;
int m_day;
};

Is an element of a class. also called, instance of the class.

1 Like
  1. they allow us to create a custom type with member variables and functions.
  2. they are functions defined inside a class
  3. a class is a more dynamic and customizable struct
  4. data type that makes up a given class.
1 Like

1. Why do we need classes, why can’t we just use structs?
Because structs are just used for data related things, inside classes we can also contain functions as a member of the class.

2. What are methods?

Methods are functions inside a class.

3. How would you define a class?
class Numbers { public: int numer; int otherNumber; };

4. What is an object?
Many things : Instance of class, variable with members or a primitive variable that caries it owned methods like String

1 Like

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

  • In OOP it’s useful to want our types to not only hold data, but provide functions that work with the data as well.

2. What are methods?

  • Functions defined inside of a class are called member functions or methods.
  • Methods can be defined inside or outside of the class definition.
class DateClass 
{
    public:
    int m_year;
    int m_month;
    int m_day;

// METHOD:
    void print()
    {
         cout << "The date today is " << m_month << "/" << m_day << "/" << m_year << endl;
    }
};

int main () 
{
    DateClass today = {2021, 04, 14};

    today.print();
    return 0;
}
// The date today is 4/14/2021

3. How would you define a class?

  • The class keyword defines a new user-defined type called a class.
  • The public keyword is also used before declaring the member variables.
// Define a class:
class Employee 
{
    // public keyword:
    public:
   // member variables and member functions
 };

4. What is an object?

  • An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated, like when an object is created, memory is allocated. So 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.
2 Likes
  1. Why do we need classes, why can’t we just use structs?
  2. What are methods?
  3. How would you define a class?
  4. What is an object?

1: we need classes because functions will not work within structs, but classes do.

2:functions within classes are called methods or member functions.

3:with the keyword class. Like this: class Example{};

4:an object is an accumulation of elements and different types+ functions within one variable. Also single members of classes are called objects.

2 Likes
  1. Structs are designed to hold data, while classes can work with the data through the use of functions.
  2. A Method is a function that is defined inside of a class.
  3. A class is a user-defined data type that not only holds data, it also provides functions that work with the data provided.
  4. An object is an instance of a class. An object is created so that the functions and data defined in the class have a value.
2 Likes

Q: Why do we need classes, why can’t we just use structs?
A: Structs are only data storing types. Classes can store functions as a member.
Q: What are methods?
A: Functions that are in classes as members are called methods.
Q: How would you define a class?
A: class Test {
public:
// inside you can define functions, objects, integers, doubles, strings, etc.
}
// or we can make them private also like this:
class Test {
private:
}
Q: What is an object?
A: It is an instance of a class. It is like a type of a class that allocates to memory.

2 Likes

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

Classes let us create a custom type in C++ that can contain both member variables and member functions. It allows for a better 'object orientation'.

What are methods?

Methods are functions defined inside of a class. They are also known as member functions.

How would you define a class?

By using the class keyword.

What is an object?

The object is defined as the entity of the class. It is both the data and function bundled together.

2 Likes

1: Functions that are able to work within the given data are the reason we use classes and not Structs.

2: Methods are a function within a given class.

3: class ivanOnTechclass {
code to be exicuted
};

4: An object is an instance of a class.

2 Likes

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

It’s recomended to use structs when we need data-only structures, but the class keyword for defining objects that require both data and functions to be bundled together. Class doesn’t allocates memory when declared until an object instantiated. So while programming we assume classes clean itself (dellocates memory) when an instantiated object removed.

What are methods?

Methods are functions defined inside of classes. They are called methods of and object. Like printForever() method inside of Fiat object.

How would you define a class?

Class is like a blueprint of an object that has features (member variables) and methods (member functions).

What is an object?

Object is an instance of a class.


Quiz

Question 1

a) Create a class called IntPair that holds two integers. This class should have two member variables to hold the integers. You should also create two member functions: one named “set” that will let you assign values to the integers, and one named “print” that will print the values of the variables.

#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 << m_a << ", " << m_b << 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;
}

b) Why should we use a class for IntPair instead of a struct?

I was thinking that the main difference is assuming those data types’ values could cause conflicts later in long lines coding if we use structs 'cos somehow values won’t get deallocated.

:wave: I need some help in here, why is that important to use class instead of struct while I get the same results exactly with the same code only changing class declaration to struct in Code::Blocks 20.03?

3 Likes

While you can define a struct as a class, there is a difference that in struct objects will be public by default while in class they will be private. Same with inheritance, and inherited struct will be public by default while class will be private.
The main reason why you should avoid using structs in a way like class in convention and readability. You should use class if the class has an invariant or use struct if the data members can vary independently. :slight_smile:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c2-use-class-if-the-class-has-an-invariant-use-struct-if-the-data-members-can-vary-independently

3 Likes
  1. Why do we need classes, why can’t we just use structs? Classes like structs allows to define a data type but also classes allow to store the member functions.
  2. What are methods? Member functions are function that are define within a class.
  3. How would you define a class? An object blueprint.
  4. What is an object? An instance of a class or struct.
2 Likes

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

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.
3. How would you define a class?
I would define a struct like a student, a function like a task or homework you might give the student (the struct) and the classroom where the magic happens under the hood.

4. What is an object?
An object is the name of the function that you call. An object can contain both data or tasks. An object is more like a label of a part of a future function to be called because the object can be defined in greater detail later in the code. All member function calls must be associated with an object of the class. When we call “today.print()”, we’re telling the compiler to call the print() member function, associated with the today object.What do m_year, m_month, and m_day actually refer to? They refer to the associated object (as determined by the caller). In this way, the associated object is essentially implicitly passed to the member function. For this reason, it is often called the implicit object . By convention, class names should begin with an upper-case letter.

2 Likes