Classes in C++ - Reading Assignment


#61

1. Why do we need classes, why can’t we just use structs?
to insert member functions
2. What are methods?
another name for member functions;
3. How would you define a class?
it is an user-defined type that provides functions that work with data

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


#62

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

We need classes to provide functions that work with the data.

We can’t just use structs, because they don’t provide enough power and flexibility.

2. What are methods?

Functions defined inside of a class.

3. How would you define a class?

class MyClass
{
public:
//variables and functions
};

4. What is an object?

A variable of a class type.


#63
  1. With classes you can store data and provide methods in one object. Structs should only be used for store data.
  2. Methods are function definded in classes.
  3. class Myclass {

    };
  4. An object is an instance of a class

#64
  1. Why do we need classes, why can’t we just use structs?
    Classes hold data and provide functions that work with the data. but structs hold only data thus classes provide much more power and flexibility.

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

  3. How would you define a class?
    class DateClass

{

public:

 int year;

 int month;

 int day;

void print()

{

cout <<year<<month <day;

}

};

  1. What is an object?
    Object is a variable of a class type

#65

1.Why do we need classes, why can’t we just use structs?
In structs, member variables only hold data. Class however, are about to hold member functions, where we can declare and assign mathematical function within a class.

2.What are methods?
Methods are functions embedded within classes.

3.How would you define a class?
A class is defined as a group of information, where we can store not only data, but also function within the class. This enables us to create objects that share all these common functions, and we need not re create every single property over and over again each time we wish to create similar objects.

What is an object?
An object is an unique instance of a particular class. That means, if the class is considered a general blueprint, then an object is when we take a copy of the blueprint and fill it with its own unique properties and details.

Here is my code for the quiz in the reading assignment, would be great if there’s any feedback on how to make it better!

class IntPair{
    public:
     int m_number1;
     int m_number2;
     void numberSet(int x, int y){
         m_number1 = x;
         m_number2 = y;

     }
     void print(){
     cout << "Pair(" << m_number1 << ", " << m_number2 << ")" << endl;
     }

};

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

    IntPair p2{2, 2};

    p1.print();
    p2.print();
    return 0;
}

#66
  1. 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. In addition to holding data, classes can also contain functions! Functions defined inside of a class are called member functions (or sometimes methods ).

  3. class theClass{

};

  1. A variable of a class type is also called an object .

#67

Why do we need classes, why can’t we just use structs? Classes and structs could actually be used to the same things, but its good practice to only use structs for data-structures and not add member functions to the struct. This because you cant be sure than all memory that has been allocated to the struct would necessary be freed. Classes are therefor used as an object with data-structures as structs, but with additional member functions that gives the objects functions that could be called if needed.

What are methods? Methods/member functions are functions declared in a class, so the class can get functions made especially for that class.

How would you define a class? class TestClass{
private:
//Often better so use private, in that would you cant change the variables inside a class object without going through a member function in the class.
public:
string m_name;
string m_location;
int m_age;
void print(){
cout<<”blablabla”<<endl;
}
};

What is an object? A variable in a class is called a object.


#68

Classes in C++ reading

1. This wasn't clear as EVERY class example works as struct ! I did some research and found members in struct are public by default and members in Class are private by default. Also you cant declare template parameters in struct. The struct keyword is just their for backward compatibility with c. 2. methods are functions in the object much like javascript. 3.class { private variables and functions public : variable list, functions }; 4.class declares an object. same as in javascript.

#69

1 A class holds data and provides functions that work with the data. Structs only work with data.

2 Methods (or member functions), are functions defined inside a class.

3

class DateClass {public: …}
class aClass {public: …}
class theRoom {public: …}

4 An object of the given class.


#70

Why do we need classes, why can’t we just use structs?
Classes can also hold functions, structs only data.

What are methods?
Functions defined inside of a class.

How would you define a class?
For example:
class DateClass
{
public:
int m_year;
int m_month;
int m_day;
};

What is an object?
A variable of a class type


#71

Why do we need classes, why can’t we just use structs?
Classes allow to create a custom type of data that can hold not only member variables, but also member functions.

What are methods?
Methods is what member functions are also known for.

How would you define a class?

class Coin{
public:
  string m_name;
  int m_circulatingSupply;
  
  void addMoreCoins(int x){
      cout << "New supply is: " << m_circulatingSupply + x << endl;
  };
};

What is an object?
An Object is a variable of the Class type, and therefore an instantiation of a Class.


#72
  1. Classes gives us much more flexibility. They can hold functions as well compare to data only structs.
  2. Methods are functions declared inside the class (they are also called Member functions).
  3. class MyClass {
    public:
    //variables
    //functions
    private:
    }
  4. A variable of class type. Can have many forms.

#73
  1. One need classes to build complex scripts without overcomplicating or duplicating codes. Classes are extended structs since they allow the use of functions.
  2. Methods are an other way of calling functions within a class.
  3. To define a class, one must use the keyword: class and give a name starting with a capital letter then curly braces to encapsulate its members and methods.
  4. An object is an instance of a class. A kind of unique copy of a class where all the given class members and methods can be used.

#74

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

To begin with structs were originally designed to only hold data whereas classes hold data and member functions. Although structs in C++ can have member functions there is a risk to doing this as a class will deallocate memory when it was destroyed whereas this may not occur with a struct which can cause memory leaks.

What are methods?

Methods is another term for member functions (i.e. functions defined inside of a class).

How would you define a class?

A class is a user defined type or data structure that has data and member functions (methods). Access to the members of a class is governed by three access specifiers (private, protected or public). The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class.

What is an object?

An object is an instance of a class data type.

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:

1

2

3

4

5

6

7

8

9

10

11

12 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;

}

and produce the output:

Pair(1, 1)

Pair(2, 2)

class IntPair

{

public:

int m_integerOne;

int m_integerTwo;

void set(int a, int b)

{

m_integerOne = a;

m_integerTwo = b;

};

void print(){

std::cout << "Pair("<<m_integerOne << ","<<m_integerTwo<<")" << endl;

};

};

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

Using a class enables us to also have member functions so that we can implicitly pass the object to the member functions. In the case of the function set we only need to explicitly pass the 2 integer values and in the case of print we don’t need to explicitly pass any values. This is a simpler and more efficient approach.


#75
  1. We 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 methods.

  3. Example:

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

    void print()
    {
    std::cout << m_year << “/” << m_month << “/” << m_day;
    {
    };

  4. An instance of a class.


#76
  1. Why do we need classes, why can’t we just use structs?
    to encapsulate variables and functions into a single object
  2. What are methods?
    are functions defined inside of a class
  3. How would you define a class?
    using the class keyword
  4. What is an object?
    an object is a variable of a class type

#77
  1. Classes allow the creation of variables and functions as well. For more complex data they can be useful to use over structs.

  2. Methods are functions that are defined within the class.

  3. A class is defined in a way very similar to a struct: class Example { public: int m_one; int m_two; int m_three}.

  4. An object is a variable of a class type. It’s also called an instance.


#78

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

Classes can contain functions as well as data. Structs are best just used for data only.

2. What are methods?

Methods are member functions, they are functions within a class.

3. How would you define a class?

class Numbers { public: int numer; int otherNumber; };

4. What is an object?

An object is a variable within a class, similare to what a member is to a struct

Quiz

#include

using namespace std;

class IntPair {
public:
int m_num1;
int m_num2;

void set(int in1, int in2){
m_num1 = in1;
m_num2 = in2;
}

void print(){
    cout << "Pair(" << m_num1 << ", " << m_num2 << ")" << endl;
}

};

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

IntPair p2{2, 2};

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

return 0;

}

1b) IntPair is a class and not a struct because structs are best suited to data only, classes can also contain functionality.


#79
  1. With structs, we can only store data. With classes we can provide functions that works with data.
  2. Functions defined inside of a class are called member functions (or sometimes methods )
  3. class name
    {
    public:
    variables
    };
  4. A variable of a class type is also called an object .

#80
  1. In c++ classes are very much like data-only structs, except that classes provide much more power and flexibility.
  2. Classes can also contain functions, functions defined inside of a class are called member functions( or sometimes methods)
  3. A class allows for us to not only hold data, we can also use functions to work with that data.
  4. A variable of a class type is called an object.