Classes in C++ - Reading Assignment


#21

1. Why do we need classes, why can’t we just use structs? Structs are generally used for holding data only, whereas classes are useful for defining objects that require both data and functions.

2. What are methods? Methods, also known as member functions are functions defined inside of a class.

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

4. What is an object? A piece of memory that’s used to store value(s) and a variable of a class type is just one example of an object.


#22
  1. Classes allow us to store member variables and member functions.
  2. Functions that are defined inside of classes.
  3. You would define a class almost exactly like a struct. class ClassName{public: /code/};
  4. A variable of a class type.

#23
  1. We often want our types to not only hold data, but provide functions that work with the data as well.

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

class ClassName{
  public:
  ...
};
  1. A variable of a class type is also called an object.

#24

1.In the object-oriented programming, we often want our types to not only hold data, but provide functions that work with the data as well.
2. Functions defined inside of a class are called member functions.
3.class ClassName
{
public:
//variables,
//functions, etc.
};
4, A variable of a class type is called an object.


#25

1. Why do we need classes, why can’t we use structs ?
Because we want types to not only hold data (struct), but functions to work with data (class).
2. What are methods?
Methods are class functions or _member functions_ .
3. How would you define a class?
With the keyword class as follows: class nameclass {};
4. What is an object?
An object is a variable of a class type. We can see the class as a blueprint of a variable type which exists only when it is instantiated, that variable is an object.
Quiz
1.

#include<iostream>

using namespace std;

class IntPair 
{
    public:
        int a_;
        int b_;
    
    void set (int a, int b)
    {
        a_ = a;
        b_ = b;
    }
    void print ()
    {
        cout << "Pair(" << a_ <<"," << b_ << ")" <<"\n";
    }
};


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

    IntPair p2{2, 2};

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

    return 0;
}
  1. Because we used two functions to work with data.

#26

1. Why do we need classes, why can’t we just use structs?
Because classes can hold functions as well as data which gives it more functionality and flexibility.
2. What are methods?
Methods are class functions.
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_month << "/" << m_day;
    }
  }

4. What is an object?
An object is an instance of a class, where we have defined a variable for a class to store class data.


#27

Classes in C++

  1. If you want to make a custom type variable with multiple data types and a lot of complex functionality, you should use classes. Structs are almost the same, but without complex functionality or methods. Structs are more a collection of different data types. while classes hold both member variables and member functions (methods). Structs are public by default while classes are private, so you must declare it public (for inheritence)

  2. Methods are functions within a class. "member functions of a class"

  3. class ClassName {} // we start the name of the class (object) with a capital letter (for easy identifcation). we declare member variables in a class with m_ prefix (m underscore) to help distinguish member variables with function variables or local variables

  4. an object is a variable of an user defined class type. ex. manzo is an object of the class Persons.

please correct my mistakes!

Greetz Manzo


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

Structs can only contain data. With classes we can also define functions acting on the data structures of the class.

  1. What are methods?

Methods are functions defined inside a class.

  1. How would you define a class?

A class is a collection of variables and functions acting on those variables.

  1. What is an object?

An object is a piece of memory on a computer.


#29
  1. A class is a user-defined type which not only holds data but provides functions that work with the data as well.
  2. Methods are functions defined inside of a class.
  3. We define a class with a keyword class.
  4. An object is variable (instance) of a class type.

#30
  1. Why do we need classes, why can’t we just use structs?
    We use them so that we can have functions inside of it although sturcts offer the same capability but it is “not” safe to use them.
  2. What are methods?
    Methods are functions in the class
  3. How would you define a class?
    By keyword Class
  4. What is an object?
    It is an instance of a class.

#31
  1. Classes allow us to use methods, i.e. functions within these structs, amongst member variables.
  2. Methods are functions which exist in structs or classes.
  3. A class is a struct which can contain methods (i.e. member functions), as well as member variables.
  4. An object is a variable of a class type.

#32
  1. We need classes and don’t use just structs when we not only want to hold data, but also want to use functions that work with that data as well.
  2. Methods are functions defined in a class.
  3. How would you define a class
    Class myClass {
    public objects and member functions or methods
    }
  4. An object is an instance or variable of a class, which also is allocated to memory.

#33

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. In C++, this is typically done via the class keyword
What are methods?
In addition to holding data, classes can also contain functions! Functions defined inside of a class are called member functions (or sometimes methods)
How would you define a class?

class classname {
public:
variables
fanctions
}

What is an object?
All member function calls must be associated with an object of the class


#34

1. Why do we need classes, why can’t we just use structs?
Classes can do more than just hold data, but also provide functions to work with this data as well.

2. What are methods?
Another name for functions defined as part of a class.

3. How would you define a class?
class Employee
{
public:
std::string m_name;
int m_id;
double m_wage;

    // Print employee information to the screen
    void print()
    {
        std::cout << "Name: " << m_name <<
                "  Id: " << m_id << 
                "  Wage: $" << m_wage << '\n'; 
    }
};

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


#35

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

What are methods?
actions performed within a class… ie functions in the class.

How would you define a class?
a data type that encapsulates properties and methods relating to a specific type of object

or

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

What is an object?
a virtual representation of a physical entity. An instantiation of a class.

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.

#include <iostream>
#include <string>

using namespace std;

class IntPair
{
public:
    int m_one = 0;
    int m_two = 0;

    void set(int one, int two)
    {
        m_one = one;
        m_two = two;
    }

    // Print employee information to the screen
    void print()
    {
        std::cout << "Pair(" << m_one << ", " << m_two << ")" << endl;
    }
};

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

	IntPair p2;
	p2.m_one = 2;
	p2.m_two = 2;

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

    return 0;
}

1b) Why should we use a class for IntPair instead of a struct?
Best practices recommended by the article we read says that if the object requires a member function you should use a class.


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

Classes allow us to use functions (methods) while structs only allow data to be organized, structs also do not do automatic garbage collection.

  1. What are methods?
    

methods are functions within a class, they automatically inherit public variables within the class.

  1. How would you define a class?
    

a struct with functions

  1. What is an object?
    

a variable that is created from a class

QUIZ

class IntPair{
public:
    int member1{0};
    int member2{0};

    void set(int x, int y){
        member1 = x;
        member2 = y;
    }

    void print(){
        std::cout << "Pair("<< member1 << ", " << member2 <<")" << std::endl;

    }
};
  1. because we need to have the set() and print() methods and structs don’t allow functions to be used.

#37
  1. Why do we need classes, why can’t we just use structs?
    Classes allocates memory and deallocates memory when being destroyed. So classes are used for data and functions both whereas structs are used for data only structures
  2. What are methods?
    Functions that are defined within classes are called methods
  3. How would you define a class?
    Using a keyword Class with c letter as capital
  4. What is an object?
    A variable of a class type is an object

#38

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.
What are methods?
Methods are the functions that are declared inside a class
How would you define a class?
as follows: class { public: ; … void {} … };
What is an object?
a variable of a class type


#39

1. Why do we need classes, why can’t we just use structs?
In addition to structs classes also contains methods (functions).
2. What are methods?
Methods are functions inside the classes.
3. How would you define a class?
class NameOfClass{
private:
//private members and methods
public:
//public members and methods
}
4. What is an object?
Object is an instance of a class.


#40

You often need functions that can act on a struct. These functions (when in a class) are called methods.

You define a class the same way as you would a struct, but replace “struct” with “class”. Also, the functions that act on the fields can go inside the class.

An object is an instance of a class.