Classes in C++ - Reading Assignment

  1. Classes allow for adding functionality to what was already provided by structs which stored only data. Although structs can also have functions it is not good to use them for this as they may not deallocate memory on destruction.

  2. Methods are functions inside a class. These functions are called methods of that class.

class{
     //content: variables and functions
};
  1. Object is an instance of a class.

Why do we need classes, why can’t we just use structs?
we need to be able to add functions as well as data storage to our types
What are methods?
member functions of classes
How would you define a class?
a structure for an object in OOAD
What is an object?
an instance of a class

  1. Classes allow functions to be carried out, structs only store data.

  2. Methods are functions defined in a class

  3. to define a class, class Employee {…};

  4. an object is a way of organising functions and data

1. Why do we need classes, why can’t we just use structs?
Classes allow us to not only create our own types to hold particular data, but they also enable us to create special functions to act on that data.

2. What are methods?
Methods are special functions that can only be used by the particular class it is a member of. For this reason they are sometimes referred to as member functions.

3. How would you define a class?
Classes are defined using the ‘class’ keyword, and are are named with a beginning upper-case letter by convention.

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?
Because Structs can only hold variables and can’t contain functions. To solve this problem, classes was created to be able for contain variables and functions.

2. What are methods?
Methods are functions defined inside the class scope, that can access every class’ member other.

3. How would you define a class?

class ClassName {
    public:
     // public members
    private:
     // private members
}

4. What is an object?
It’s an instance of a class. Therefore, every time that a class is instantiate, the program allocate space in memory to hold the new object that was created.

1 Like
  1. structs are merely a collection of data, whereas classes allow us to provide functions that can be applied to this data. Classes also have better support for import Object-Oriented Programming concepts like constructors/destructors, encapsulation and heritage.
  2. Methods are the functions in a class.
  3. Classes are defined with the class keyword, e.g.:
class MyClass {
public:
    int x;
    int y;
    void foo();
};

Besides public, there are also the access specifiers protected and private.
4. An instance of a class (that is, a variable of the class type) is called object.

1 Like
  1. We need classes because structs only hold data types. Classes allow us to do that and also add functions that can manipulate our data types.
  2. Methods are functions within our class.
  3. class Person{
      public:
          string name;
          int age;
          void printPerson() { cout << name << " " << age << end; }
    };
    
  4. An object is a variable of our class type.
2 Likes

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

Unlike structs in which the program only consists only of the storing of data, classes (as similar to structs) enable the programmer to additionally include functions that will operate with the declared data in the class

2. What are methods?

Methods are another word for functions. A methods functions to operate with the data in either inside the class or on the outside.

3. How would you define a class?

The syntax of a class is as following:

class Month {
public:
string name;
int numberOfDays;
};
Month july {“July”, 17};
cout<<july.name<<" has "<<july.days<<endl;

4. What is an object?

An object is another term for the variable that is stored in the class in which it defines an instance of a class.

2 Likes

1.Why do we need classes, why can’t we just use structs?
We need classes when in our custom data type we have variables and methods. If we don t have methods we can use structs , but when one or more members of the custom data type is a method or function we have to use classes.

  1. What are methods?
    Methods are functions that we use inside of our classes.they are member functions.

  2. How would you define a class?
    A class is a custom data type that cointain in their members variables and functions.

  3. What is an object?
    An object is an instance of my class.

1 Like
  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 (as is the case for structs), but provide functions that work with the data as well. In C++, this is typically done via the class keyword. Using the class keyword defines a new user-defined type called a class.

  2. 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).

  3. How would you define a class?
    Example for a date Class:
    Here is our Date class with a member function to print the date:

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

void print() // defines a member function named print()
{
    std::cout << m_year << "/" << m_month << "/" << m_day;
}

};
Just like with a struct, to use a class, a variable of that class type must be declared:
DateClass today { 2018, 07, 21 };
In C++, when we define a variable of a class, we call it instantiating the class.

  1. What is an object?
    In C++, 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.
1 Like
  1. Because classes allow us to include functions as well, in comparison to structs, that can be used only for storing data, not functions
  2. Methods are functions that are accessible only in a specific class, and can be used only with an object from that class
  3. It is very similar to struct, so it is grouping together objects with the same properties and characteristics, and it not only allows for storing data, but also for writing functions to interact with that data
  4. Object is one specific representative of a class, a member of that class, that has all the data-properties defined in that class, and has access to all the member functions from that class
1 Like
  1. We need classes to declare groups of values and the associated functions that will be commonly executed on them under one logical name. You can access the members of a class the same way as structs with dot notation.

  2. Methods are members of a class that are functions. Methods have local access to all of the classes variables without having to pass them in as parameters. This saves us time when programming.

  3. You define a class by using the class keyword and including its members within curly braces.

class someClass {
    public:
        int m_someInt;
        string m_someString;

    void printData() {
        cout << someString << someInt << endl;
    }
};
  1. An object is an instance of a class type.
1 Like
  1. Structs can only hold data, classes can also hold functions to operate with that that data.
  2. Methods are functions that operate with class data. Methods can be defined inside or outside of our class. Methods are called with . (dot) operator just like other members of the class.
  3. class myClass {
    public:
    int x = 10;
    string test = “test”;
    }
  4. Object is an instance of a class
1 Like

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

Because of flexibility. The data type class is more flexible since it can include functions
that manipulate information of the members of the class . In other words, are more flexible because you can create and customize types of variables according to your needs.

What are methods?
They are functions defined INSIDE a class. The parameters of such functions are members of the class.

For instance, in

class DateClass
{
public:
    int m_year;
    int m_month;
    int m_day;
 
    void print()
    {
        std::cout << m_year << "/" << m_month << "/" << m_day;
    }
};

print is a method.

How would you define a class?
A custom-made structure that can also contain functions.

What is an object?
It is an instantiation of a type class variable.

For instance in

std::array<int, 3> a { 1, 2, 3 };

you create an array object.

1 Like

1. Why do we need classes, why can’t we just use structs?
Classes are very much like data-only structs, except that classes provide much more power and flexibility

2. 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).

3. How would you define a class?
eg
class YearClass
{
public:
int m_day;
int m_week;
int m_year;
};

4. What is an object?
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.

quiz
1a
#include

using namespace std;

class IntPair
{
public:
int m_one;
int m_two;

void set(int one, int two)
{
	m_one = one;
	m_two = two;
}
void print()
{
	// create format Pair (x,x)
	cout << "Pair(" << m_one << " , " << m_two << ")"<<endl;
}

};

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

IntPair p2{ 2, 2 };

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

return 0;

}

1b
Because this object includes both member data and member functions.

2 Likes
  1. Structs are only used to store data, with classes you can store both data and functions.

  2. Methods are functions stored inside of classes.

  3. Example:
    class Students {
    public:
    string m_fullName;
    string m_id;

void print() {
cout << "Student name: " << m_fullName << endl;
cout << "Student ID: " << m_id << endl;
}

};

  1. An object is a variable that you have declared(instantiated) of a class.
1 Like

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. In C++, this is typically done via the class keyword. Using the class keyword defines a new user-defined type called a class.
In C++, classes are very much like data-only structs, except that classes provide much more power and flexibility.

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

3. How would you define a class?
An example of a class definition will be as follow:

class DateClass
{
public:
    int m_year;
    int m_month;
    int m_day;
 
    void print()
    {
        std::cout << m_year << "/" << m_month << "/" << m_day;
    }
};

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

2 Likes
  1. We need classes because with classes they do not only store data but we can also provide functions.

  2. Methods are functions defined inside a class.

  3. class Class name {
    public:

private:
};

  1. An object is an instance of a class.
1 Like

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

  • classes can contain both variables and function
    What are methods?
  • methods are function defined in class
    How would you define a class?
  • class abc{
    public:
    …
    }
    What is an object?
  • a variable of a class type
1 Like
  1. Classes allow to contain and execute functions, unlike structs.
  2. Methods are functions inside a class.
  3. Like this: class MyClass {…};
  4. Any instance inside a class.
1 Like