Classes in C++ - Reading Assignment

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

Structs are typically used to hold data only, although structs may contain functions. Classes provide a way to hold data and functions that work with the data as well. Data only structs need the functions to be defined separately. Classes will also dealocate memory after being destroyed, which increases performance, you may not assume this using a struct.

2. What are methods?

Methods or member functions are functions inside a class that use the data inside the class.

3. How would you define a class?

First declare the class

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

then declare the variables of the class using the known method:
DateClass today { 2020, 10, 14 };

4. What is an object?

An object is a variable of a class type, also called an instance

1. Why do we need classes, why can’t we just use structs?
Classes allow for functions to be included. Structs can only hold data.
2. What are methods?
Functions which are a member of a class.
3. How would you define a class?
class TestClass {
code goes here…
};
4. What is an object?
An object is an instance of a class.

1. Why do we need classes, why can’t we just use structs? Use the struct keyword for data-only structures. They can contain functions, but they do not implicitly pass the struct object that is inside the struct. You need to explicitly pass the object to the function (ie: class usage of member function: today.print(); vs struct function usage: print(today):wink: In the struct use of the function, you need to pass the struct to the function, otherwise it will not know what to do. Versus the class use of the member function, it’s dot notation implicitly passes the class object into the function. Use the class keyword for objects that have both data and functions
2. What are methods? Methods are functions that are within classes or structs .
3. How would you define a class? You can define a class the same way as a struct, the convention is to name it with initial caps and classes can have private and public member variables.
4. What is an object? A variable of a class type is also called an object

  1. Structs on their own are not that useful as they only contain data but don’t have a way to alter that data. Classes allow programmers to take data as inputs (e.g structs, arrays, primitive data types) and manipulate the state of the data in order to produce some behaviour.

  2. Methods are member functions within a class which can take input and manipulate it order to produce some specific behaviour or functionality

  3. class MyClass {
    // member variables

// member functions
void myMethod() {
// do something to the variables
}
};

  1. An object is an instance of a class. For example, a programmer could create a class called Person. They could then create instances of that Person class which are the objects and can represent things in the real world e.g. John, Sarah e.t.c
  1. Because structs are data-only whereas classes can embed functions
  2. Methods are functions that belong to a class or at least that relate to a class
  3. A class is a model, like a struct for describing properties but with tools (methods) in addition
  4. An object is an instantiation of a class which means this is a concrete realization of a class. A class is an abstraction, a set of properties and methods whereas an object is a concrete materialization of this abstraction, it’s an exemple of applied set of properties and methods.
  1. Why do we need classes, why can’t we just use structs?
    Classes hold member variables and member functions and structs hold only variables.

  2. What are methods?
    Member functions defined inside of a class.

  3. How would you define a class?
    Class Date {
    Public:
    Int m_year;
    Int m_month;
    Int m_day;

    Void printDate() {
    Cout << m_year << “/” << “/” << m_day;
    }
    }

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

1 - classes allow to integrate functions inside them, using parameters from inside the same class.
2 - methods are functions defined inside a class, and they can use variables from inside the same class.
3 - class {
//access specifiers
// variables
// methods
};
4 - an object is an instantiation of a class.

  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.
  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 ). Member functions can be defined inside or outside of the class definition.
  3. How would you define a class? A struct that not only hold data, but can also provide functions.
  4. What is an object? when we define a variable of a class, we call it instantiating the class. The variable itself is called an instance , of the class. A variable of a class type is also called an object, for example: DateClass today { 2020, 10, 14 }; // declare the variable or objet “today” of class “DateClass”.strong text

CAN SOMEONE ANSWER THIS PLEASE!?!?

So after getting 50% of the course done I just noticed this for the 1st time.

At the bottom of the article you’ll see a quiz - make sure to do the quiz!

I mean I have been answering them, but I wasn’t posting them on the quiz page, just solving them in my or taking notes.
Will this be a problem?

#letsgetthiscrypto

  1. With classes, we can provide functions that works with the data… structs only store the data.
  2. Functions defined in classes are methods.
  3. class class DateClass{
    …
    };
  4. An object is an instance of a class.

#letsgetthiscrypto

1. Why do we need classes, why can’t we just use structs?
Because structs are data only. In addition to holding data, classes can also contain functions. Functions defined inside of a class are called member functions (or methods ).

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

3. How would you define a class?
class ClassName
{
public:
public members (variables and functions/methods)
private:
private members (variables and functions/methods).
}

4. What is an object?
An object is an instance of a class. A variable of the type class allocated to memory.

Classes in C++ - Reading Assignment

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

  • it is better to use class when using member functions
  • structures only can hold data, no functions

2. What are methods?

  • member functions can be called methods

3. How would you define a class?

class classname{
public:
    int var;
    int function(){
    code
    }
    ...
};

4. What is an object?

  • objects are the variables of classes
  1. Why do we need classes, why can’t we just use structs?

Structs are used to represent a single value, classes can have member functions

  1. What are methods? Function inside a class (member functions
  2. How would you define a class?

Class name {

Public, private or protected :

Data to be used

Member funtions

}

  1. What is an object?

A variable of a class type

  1. Why do we need classes, why can’t we just use structs?
    structs do not allow functions, they are only for data. Classes will can have functions and data.

  2. What are methods?
    Functions that are within a current a class.

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

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

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

It’s fair to assume a class will clean up after itself (e.g. a class that allocates memory will deallocate it before being destroyed), but it’s not safe to assume a struct will. Consequently, we recommend using the struct keyword for data-only structures, and the class keyword for defining objects that require both data and functions to be bundled together.

  1. What are methods?

Those are member functions associated with class. They can be created inside or outside the class.

  1. How would you define a class?

The same way as struct, but with adding access specifier.

class IntPair{
public: // public, private or protected
    int m_number1;
    int m_number2;
    void set(int num1, int num2){
        m_number1 = num1;
        m_number2 = num2;
    };
    void print(){
        cout << "The first number is " << m_number1 << endl;
        cout << "The second number is " << m_number2 << endl;
    };
};
  1. What is an object?

Variables and functions grouped into single unit.

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

Structs don’t necessarily release memory used; recommended to hold data only

2. What are methods?

Actions within a class that perform operations on data

3. How would you define a class?

Using the class keyword, data and methods can be within a class

4. What is an object?

An object is an instantiation of a class

  1. Why do we need classes, why can’t we just use structs?
    because strcuts can only hold data and not functions.

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

  3. How would you define a class?
    We put the keyword “class” and “ClassName” and data members and function members of the class would be placed inside the brackets :
    class ClassName
    {
    // data members and function members
    };

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

  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.

Quiz time
1a) 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.
The following main function should execute:

#include
#include

using namespace std;

//Create a class called IntPair that holds two integers.
//This class should have two member variables to hold the integers.
//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.

class IntPair
{
public:
int m_first;
int m_second;

void set(int first, int second)
{
	m_first = first;
	m_second = second;
}
void print()
{
	cout << "Pair(" << m_first << ", " << m_second << ")\n";
}

};

int main()
{
IntPair p1;
p1.set(1, 1);

IntPair p2{ 2, 2 };

p1.print();
p2.print();

return 0;

}

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

This object contains both member data and member functions, so we should use a class. We should not use structs for objects that have member functions.

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

A: Classes are used to create user defined types. The language only supplies a few to get you started ( int, signed, unsigned, char, bool, and etc…) there is enum, struc for the similar purpose, but not enough to provide a good way to manage your codes.For a small program you might not really need it.It really depends on the programmer when to use it.Classes may not be needed as long as there are reused pairs of functions but As a number of functions in program grows.Classes comes in mind.Classes cannot do anything without functions except for the holding data.

  1. Q: What are methods?

A: Functions defined inside of a class are called member functions or sometimes methods.

  1. Q: How would you define a class?

A: 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.

  1. Q: What is an object?

A: 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. Why do we need classes, why can’t we just use structs?
    Structs, over time, have accumulated functionality of classes. However, after the Struct is destroyed it may not deallocate its data while a class will. Also the paradigm for structs are supposed to be used for data while classes are made to carry and manipulate data.

  2. What are methods?
    Methods are for the manipulation of data within an object.

  3. How would you define a class?
    class ClassName
    {
    Visibility Modifier:
    // m_variable_name the m_ is for distinguishing a member variable from its input

    void methodName()
    {
    // do something with either input or present data
    }
    };

  4. What is an object?
    Instance of a data structure (class or struct)