Classes in C++ - Reading Assignment

1.we can use functions with the class, but with structs it only stores data

2.funtions in the class

  1. class NAME{};

4.when a class is called

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

For matters of convenience, classes are better to implement that structs when you have to store data and functions into an objects. Structs are better to use when you’re only dealing with data.

  1. What are methods?

Methods are functions defined within a class.

  1. How would you define a class?

class Thing {}

  1. 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?
    We need class for defining objects that require both data and functions to be bundled together. Structs are best used for for data-only structures.

  2. What are methods?
    Functions defined inside of a class.

  3. How would you define a class?
    Like a blueprint. They describe what the resulting object will look like.

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

1 Like
  1. Classes are used for objects that hold both data and functions, while structs are used for data-only structures.

  2. Methods, or member functions, are functions declared inside of a class.

  3. Classes are commands that define new user-defined types. Classes can hold both data and functions.

  4. The object is the actual entity inside the blueprint, which is the class.

1 Like
  1. We use classes to hold data and also functions that work with the data, whereas structs are used to hold data.

  2. Class functions are called methods or member functions.

3.Class functions may be defined inside or outside of the class definition and are accessed using the member selector operator, (.) e.g.

class Person{

    public:
        string name;
        int age;
      

        void printInfo(){
            cout<<"Person's name is "<<name<<endl;
            cout<<"Person's age is "<<age<<" years"<<endl;
        }

};

  1. An object is an instantiation of the class, e.g.
int main()
{
    Person human={"Human",99};
    human.printInfo();
    return 0;
}
1 Like
  1. Why do we need classes, why can’t we just use structs?
    they allow functions to be used and not just storing data like as in Structs
  2. What are methods?
    functions that are called on inside of a class
  3. How would you define a class?
    start with ā€œclassā€ then add a name of the class starting with a capital letter as example ā€œEmployeeā€
  4. What is an object?
    An object is an element of a class
1 Like
  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.
1 Like
  1. Structs are used exclusively for data, while classes contain both member variables and functions. We could technically add methods to structs as well, however that is not done conventionally and also not recommended for memory allocation reasons.
  2. Methods are member functions embedded in classes.
  3. Example below
class MyClass{
public:
//member variables, methods
private:
//member variables, methods
};
  1. An instance of a class.
1 Like
  1. Structs dont have functions on them, classes can provide functions to work with data, so this makes them more versatile.
  2. Methods are functions defined inside of a class, they are also called member functions.
  3. class MyClass { ... };
  4. An instance of a class with allocated memory
1 Like

Questions:
. Classes allow for functions which can work well with data and not just store it.
. Methods are functions defined inside of a class.
. Class nameOfClass
{
};
. An object is a type of class.

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

Classes provide the option of having methods or functions inside them that can operate on external data passed into them or on the member variables of the class they are a part of.

  1. What are methods?

Methods are functions that belong to a class. They can only be called on objects of a particular class only after that object has been initialized and only using the . operator

  1. How would you define a class?

A class is a data type defined by the programmer containing several member variables that determine the state of the object of the class. For example a class representing a vehicle could have member variables representing the make, model, and year along with possibly more member variables that determine the state of any car object.

The class also contains methods, which are functions defined inside the class that can operate on data external to the class as well as on the internal data i.e. the member variables to perform some associated action.

  1. What is an object?

An object is a specific instance of the class i.e. a class that exists in memory and has been initialized such that its members have specific values and its methods can be called via the dot operator (.) and the object name which the user chose.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Now both, structs and classes can have member functions, but in the past, structs could not have member functions. Even though they both can accommodate member functions, it is still recommendable to use classes for member functions since it handles memory allocation much better than structs do.
  2. What are methods?
    Methods and member functions and kind of the same, functions that are define within a class
  3. How would you define a class?
    A class is a type of object container that can accommodate different type of objects, such as variables, data types, functions, and other structs or classes.
  4. What is an object?
    An object is a region of storage/memory that has a value and other properties. Variables, structs, classes, etc. are examples of objects.

Quiz time

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.

The following main function should execute:

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)

Solution:

#include

class IntPair
{
public:
int m_a{};
int m_b{};

int set(int a, int b)
{
    m_a=a;
    m_b=b;

    return 0;
}

int print()
{
    std::cout<<"Pair("<<m_a<<", "<<m_b<<")\n";
    return 0;
}

};

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

IntPair p2 { 2, 2 };

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

return 0;

}

1 Like
  1. Classes allow for the ability of functions to actually work its our data, whereas structs only allow for the holding of data.
  2. Methods are simply functions that are defined within a class.
class thisClass{
      public: ……….
      …………;
      private: ……………..;
}
  1. An instance or element of a class.
1 Like
  1. Structs are good for data-only structures but classes are better suited for objects that contain data and functions.

  2. Methods are member functions. They are functions that are defined inside of a class.

  3. Classes are defined the same way structs are defined. The only major difference is that classes will have the public or private keywords to specify access.

  4. When you define a class, you are creating a blueprint that you can later use to build objects. You create objects by specifying a class and giving them data members and methods.

1 Like

1. Why do we need classes, why can’t we just use structs?
Structs are used to store data, while classes have functions and data.
2. What are methods?
functions that are members in a class.
3. How would you define a class?
class SampleClass { define variables / methods};

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

1 Like

1. Why do we need classes, why can’t we just use structs?
We can employ member functions in classes as well as data.

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

3. How would you define a class?
A data structure which can include both variables and member functions

4. What is an object?
A variable (instance) of a class type

1 Like

1. Why do we need classes, why can’t we just use structs?
Because in some situations in object-oriented programming, programmers do not want types to only hold data, but to hold functioins that can operate on the data. This is where classes come in. the class keyword creates a new user-defined data-type called class.
2. What are methods?
Functions defined within a class (member functions)
3. How would you define a class?
The same way as you define a struct, though with the addition of the keyword ā€˜public’ or ā€˜private’ to indicate access.

class ExampleClass
{
public: // defines accessability 
    int value {};
    void function ()
    {
       executable code
    }
};

4. What is an object?
An instance of a class, so if I take the above ExampleClass and declare it with values, that is an ExampleClass object.

1 Like
  1. Classes can store functions and data while structs only store data.
  2. A method is a function that is defined within a class.
  3. A class is an object that can store any type of data as well as funcitons.
  4. An object is an instance of a class.
1 Like
  1. Classes can define functions aka methods that work with the data contained in the class, while structs do not.
  2. Methods are functions defined within the class.
  3. class theClass {stuff you want in theClass}
  4. An object is a variable/instance of a given class type.
1 Like

1. Why do we need classes, why can’t we just use structs?
when they only hold variables they are exactly the same, with only one difference: classes need to specify if the variables are public or private (scope). But classes can also contain functions as members, and these are called methods.

after reading the whole lesson… turns out I was wrong:

ā€œIn C, structs can only hold data, and do not have associated member functions. In C++, after designing classes (using the class keyword), Bjarne Stroustrup spent some amount of time considering whether structs (which were inherited from C) should be granted the ability to have member functions. Upon consideration, he determined that they should, in part to have a unified ruleset for both. So although we wrote the above programs using the class keyword, we could have used the struct keyword instead.ā€

Use the struct keyword for data-only structures. Use the class keyword for objects that have both data and functions.

2. What are methods?

Methods are members of a class, but the member type is a function.

3. How would you define a class?

class Student {
public :
    string name{};
    int age{};
    string email{};
    string courses[]{c++, bitcoin101};

void printStudentInfo() {
     std::cout<<"Student "<<name<<" is "<<age<<" years old, his email is "<<email<<" and has taken the following courses:"<<endl<<courses[0]<<endl<<courses[1]<<endl;
}

private :
   float grades[]{6.5, 5.9};
};

4. What is an object?

Class is mere a blueprint or a template. No storage is assigned when we define a class. Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.

Each object has different data variables. Objects are initialized using special class functions called Constructors . And whenever the object is out of its scope, another special class member function called Destructor is called, to release the memory reserved by the object.

1 Like