Classes in C++ - Reading Assignment

  1. Classes can be used to not only hold data but it also provides functions that work with the data as well.
  2. Functions defined inside of a class are called member functions, also METHODS.
  3. The class keyword defines a new user-defined type called a class.
  4. An object is an instance of a class.
1 Like

1. Classes provide much more power and flexibility. For example, classes can also contain functions and public or private variables.

2. Functions defined inside of a class.

3. class SampleClass { // define variables and/or methods here };

4. An instance of a class with allocated memory.

1 Like
  1. We might use structs instead of classes because it can hold not only data, but provide functions that work with the data as well.
  2. Functions defined inside of a class are called methods
  3. using the keyword Class
  4. an object is a component of a class that has memory allocated to it
1 Like

1. Why do we need classes, why can’t we just use structs?
We need classes to do object oriented programming, to have custom data types with both data and functions/methods.
2. What are methods?
Functions as class members.
3. How would you define a class?

class ClassName
{
    public:
        //Data
        type classMember;
        //Method
        returnType method(param1, param2, ...)
        {
            statements;
        }
};

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

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Structs are great for storing strictly data only values. Classes give us the ability to be able to use functions along with the data being stored.

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

  3. How would you define a class?
    class Nameofclass {
    public, private or protected

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

1 Like
  1. We need classes in order to solve more complex problems that is difficult to solve with structs ,as structs only hold data while classes provide functions that work with the data as well.

  2. Methods (member functions) are functions defined inside of the class definition.

  3. )we define a class from:
    class keyword ,
    public: keyword ,
    “m_” prefix

  4. Object is an instance of our class type.

1 Like

1 - Structs are only useful for handling and storing data, whereas with classes we are able to do this while also being to provide functions that work using the same data.
2 - Functions defined inside of a class are known as member functions/ methods. These are useful as they can be defined inside or outside the class.
3 -
Class
{
private/public:
members; // public or private
members; // public or private
members; // public or private

};

4 - Objects are instances of a class.

1 Like
  1. 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.

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

  3. How would you define a class?
    class ObjectName{
    public:
    type name of member{};
    type name of member{};
    type name of member{};

    member function(){

    }
    }; <<<<<<— IMPORTANT TO NOT FORGET THE SEMI COLON

  4. What is an object?
    An object is a member or an “instance” of a class . An object has a state in which all of its properties have values that you either explicitly define or that are defined by default settings.

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

With classes we can not only store data structures but also process them through functions that we can declare inside the class.

  1. What are methods?

Methods are functions declared inside a class. Methods can be called with help of the member selector operator (.)!

  1. How would you define a class?

Class DateClass{
//doSomethinghere
};

  1. What is an object?

An object is a defined class. Until a class, or struct, is not defined it does not allocate memory and is simply a blueprint that tells us how this datatype looks like.

1 Like
  1. " Contrary to a struct , a class is made to offer an interface , that has some degree of separation from its implementation. A class is not just there to store data. In fact a user of a class is not supposed to know what data the class is storing, or if it contains any data at all for that matter. All he cares about is its responsibilities, expressed via its interface.
    A class raise the level of abstraction between interface and implementation even more than a struct does." Source: https://www.fluentcpp.com/2017/06/13/the-real-difference-between-struct-class/

  2. Methods are functions defined inside a class.

  3. Classes are defined as strcuts:
    class ClassName{
    member variables and functions
    };

  4. An object is an instance of a class, which is created when variables of the class are assigned.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Structs can only hold data. With classes we can use methods as well.

  2. What are methods?
    Method is the function within a class.

  3. How would you define a class?
    A class is an object created and defined by the user to use in the program.

  4. What is an object?
    Variable of an user defined class.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    With structs we can only store data soo classes were introducted and they also contain member functions that can be called inside object
  2. What are methods?
    Methods are function which are executed upon the data belonging to the class objects instance
  3. How would you define a class?
    class Example{
    public:
    int m_var1;
    double m_var2;

function(){
}
};

  1. What is an object?
    Object is class type of variable
1 Like
  1. We need classes in defining objects that require both data and functions to be bundled together because we can safely assume that a class that allocates memory will de-allocate it before being destroyed whereas a struct would not hence, we use a struct in defining objects that holds only data.

  2. Methods are functions define inside a class (and struct), also known as member functions.

  3. By the use of the keyword class as shown;
    class Person{
    public:
    string name{};
    int age{};
    int height{};
    };

  4. An object is an instance of a class.

1 Like
  1. Why do we need classes, why can’t we just use structs? — to support function() done on the struct, classes are like structs with built in functions
  2. What are methods? — functions of classes, also known as member functions
  3. How would you define a class? — if struct is a user defined type, class is a user defined data structure
  4. What is an object? — declared instance of a class
1 Like

1 Why do we need classes, why can’t we just use structs? So we can not only hold data like a Struct but we can also build member functions too.

2. What are methods? Functions defined inside a class

3. How would you define a class? using a user defined class keyword.

4. What is an object? is a variable of a user defined class

1 Like
  1. You cannot create functions inside structs.
  2. Functions defined inside the class
  3. For example

class Animal
{
public:
string species{};
int age{};
};

  1. Instance of a class
1 Like
  1. Unlike classes, we cannot define functions outside a struct like we can with classes. This allows for us to use functions defined outside the class instead of having to define a function inside the struct. In addition to beign able to use functions from outside the struct, we can also call a function that has been defined underneath the call expression where as in a struct, we would need to define the function first and then call the function after it’s been defined.

  2. A method is a function defined inside a class.

  3. Classes are defined in the below manner:
    class DateHire
    {
    public:
    int m_year {};
    int m_month {};
    int m_day{};
    };

  4. An object is an instantiated variable within the type class.

1 Like
  1. Structs cannot contain functions as members whereas a class can contain functions as well as hold data
  2. functions defined inside of a class are called member functions or methods
  3. class Person{
    public:
    string name{};
    int age{};
    };
  4. An instance of a class
1 Like
  1. Why do we need classes, why can’t we just use structs? to add functions that work with the data
  2. What are methods? member functions
  3. How would you define a class? using the class keyword instead of struct
  4. What is an object? an instance of the class
1 Like
  1. Because structs do not allow for functions like classes do
  2. functions inside of a class
  3. class MyClass {
    public:
    };
  4. an instance of a class, field, or variable
1 Like