Classes in C++ - Reading Assignment

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

Because more complex data types are often not easily handled in structs. Structs don’t have all the same memory allocation properties that classes do.

  1. What are methods?

A function or a procedure dedicated to a class.

  1. How would you define a class?

class myClass {
private
// fields
public
// fields
methods
void myFunction();
};

  1. What is an object?

An instance of a class. Classes represent data types, but an instance of a class must be declared in memory. One class can be made into several objects of the same class. Once you have a class defined, you can inherit it’s methods and properties by defining a class of another class. This avoids the necessity for code duplication.

Objects allocate memory and it’s important to remember that any objects that you create must also be freed or one can have memory leaks. This is the case in Pascal, but so far in C++ in the course, this issue hasn’t been addressed.

  1. Why do we need classes, why can’t we just use structs?
    Structs hold only data and need to be passed into functions in order to do work on that data. With classes we can encapsulate specific functions within the class that expose methods by which we can retrieve or work on the contained data as required.

  2. What are methods?
    Methods are functions that are part of the class structure.

  3. How would you define a class?
    We define a class by using the class keyword followed by the name and curly brackets. The data structure and member functions are then defined within the curly brackets.

  4. What is an object?
    An object is an area of memory used to store values. When it is named, it becomes a variable. From an object-oriented perspective it can be thought of as a self encapsulated, and reusable, variable that contains properties (data) and behaviours (methods).

  1. We need classes when we want to make our own functions. If we deal with data only we can use a struct, but if we are dealing with data and functions at the same time, we should always use a class.

  2. Methods are functions defined in classes

  3. class {
    //access specifiers
    // variables
    // methods
    };

  4. A data type that is defined by a given class.

  1. With a class you can define a function as a member.

  2. Methods are also known as member functions. This is the reason to use a class instead of a struct.

  3. class DateClass // Use a capital for class name
    { // Use curly brackets
    public: // Declare it public
    int m_year;
    int m_month;
    int m_day;

    void print() // defines a member function named print() // Add a member function / method
    {
    std::cout << m_year << “/” << m_month << “/” << m_day;
    }
    }; // Do not forget the semicolon!

  4. An object is an instance of a class. To actually create an object of the class, a variable of that class type must be defined like this (for ex.): DateClass today { 2019, 12, 21 }; // declare a variable of class DateClass

image

  1. To allow functions to go inside of the class which will use the data in the class.
  2. They are functions.
  3. class Name{};
  4. It is an instance of a class
  1. in contrast to structs, you can also define functions(methods) in classes
  2. functions in classes
  3. class Employee { // variables .... // methods .... };
  4. instance of a class

Because structs doesn’t provide the keyword public

Methods are member functions. Member functions are functions definied in a class.

class MyClass {
public:
(member variables and or member functions)
};

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.

  1. Why do we need classes, why can’t we just use structs?
    Unlike structs, with classes we can do more than just store data, we can provide functions to work with that data.

  2. What are methods?
    Methods or member functions are functions defined inside of a class.

  3. How would you define a class?
    class TestClass
    {
    public:
    variables;
    functions/methods;
    };

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

  1. Recommendation is to use structs with data-only structures and Classes for defining objects that require both data and functions to be merged together.
  2. Methods are also known as member functions can be defined inside or outside of class definition. All member function or methods must be associated with an object of the class.
  3. class Class
    {
    public: // public member variables and member functions
    };
  4. Object can be many things like string, array, vector, int. It is instance of the class.
1 Like

1) Why do we need classes? Why can’t we just use structs?
We need the class keyword for defining objects that require both data and functions to be bundled together.
2) What are methods?
Methods - also called member functions - are functions defined inside of a class.
3) How would you define a class?
class MyClass {
public:
// public member variables and functions
};
4) What is an object?
an object is a variable of the class type.

1 Like

1 Classes allow us to define member functions (methods) inside, structs are only for data.
2 The functions inside a class.
3 A data structure that enables you to create objects and is the core of object oriented programming.
4 A data type that is defined by a given class.

1 Like

Quiz 1

#include <iostream>
//#include <string>

using namespace std;

class IntPair
{
public:
    int x;
    int y;
    
    
 
    // Print employee information to the screen
    void set(int x, int y){
        this->x=x;
        this->y=y;
    }
    void print()
    {
        std::cout << "Pair(" << this->x<<  ","<< this->y<<")\n";
        
        
    }
};

/*
 * 
 */
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;
}

@gabba Edit: you can use the Preformatted Text, click on the </> icon @cherrybluemoon to display code :wink:Preformatted text

1 Like

Why do we need classes, why can’t we just use structs? A class declaration does not allocate any memory.
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.
How would you define a class? Class keyword defines a new user-defined type. Use the class keyword for defining objects that require both data and functions to be bundled together.
What is an object? Object are data and functions to be bundled together.

1 Like

@gabba Regarding Edit: Wow, I had no idea. Thank you for pointing this out! :smile:

  1. Why do we need classes, why can’t we just use structs?
    With structs, we can only store data. With classes we can also provide functions that work with data.

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

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

    };

  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?

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.

2. What are methods?

In addition to holding data, classes (and structs) can also contain functions! Functions defined inside of a class are called member functions (or sometimes methods ).

3. How would you define a class?

class DateClass

{

public:

int m_year;

int m_month;

int m_day;

};

4. What is an object?

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.

1 Like
  1. Why do we need classes, why can’t we just use structs? Classes are preferred if we wish to include member functions.

  2. What are methods? Functions declared within a class as a member.

  3. How would you define a class?
    MyClass myObject { "some text", 10, true };

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

QUIZ:

class IntPair{
public:
    int m_Anum;
    int m_Bnum;
    void set(int a,int b){
    m_Anum = a;
    m_Bnum = b;
    };
    void print(){
    cout << "Pair(" << m_Anum << ", " << m_Bnum << ")" << 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;
}
1 Like
  1. Classes allows functions to be defined that perform operations on the members of the class.
  2. Member functions defined as part of a class are known as methods.
class ClassName {
         public:
             int a;

        void functionName {
        }             
};
  1. An instance of a particular class.
1 Like