Classes in C++ - Reading Assignment

  1. Why do we need classes, why can’t we just use structs?
    For convention structs are for data, classes are for data and functions.
  2. What are methods?
    Methods are the function in a class
  3. How would you define a class?
    With the key word class (it is similar to struct).
    Like so:
class Ball {
public:
  double size;
  string color;
  void print() {
    std::cout << "ball info: " << "\n" ;
    std::cout << " size: " << size << "\n";
    std::cout << " color: " << color << "\n";
  };
}
  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? Structs dont have functions, you only have information (data type). Classes have functions and code.

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

  3. How would you define a class? class Classname

  4. What is an object? Datatype with in a class

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

    • Structs can only store data while Classes can provide functions that works with the data as well.
  2. What are methods?

    • these are functions defined inside of a Class.
  3. How would you define a class?

    • A class is very identical with struct but the only difference is it can provide functions also for the data it holds.
  4. What is an object?

    • an instance of a class
1 Like
  1. structs can only store data whereas classes can also provide functions whith the data
    2.methods are functions defined in classes
  2. class Pizza{

    }
  3. class,variable with members…
1 Like

1.Why do we need classes, why can?t we just use structs?
Class allow us to hold function. This is useful in object-oriented programming:
2.What are methods?
Methods are inside of a class defined functions.
3.How would you define a class?
Classes are similar to structs, the difference is that we can use Functions.
eg:

Calss className
{
access_specifier: // can be private, public or protected
Class members; //variables to be used
Member Functions() {}
};

4.What is an object?
An object is created from variables and function which belongs to a class.

1 Like
  1. Structs can only contain data, whereas classes and store functions as well
  2. Methods are functions thats are members of a class

Class classOne{
public:
int 1;
string name;
int 2;
string place;
};

etc

  1. An object is an instance of a class that has been allocated memory
1 Like
  1. Why do we need classes, why can’t we just use structs?

It provides functions that work with data

  1. What are methods?

Functions defined inside a class

  1. How would you define a class?

Similarly to struct, but you need to remember public:

  1. What is an object?

Object is an entity that has state or behavior.

1 Like
  1. Structs are for data, classes combines the data with functions as well (or simply functions)
  2. Functions belonging to a class (defined within it)
  3. class{
    objects (variables, other properties or data)
    functions;
    }
  4. an instance of a data type defined by class
1 Like
  1. It may be necessary that the new type requires functions to work with the data.
  2. Methods are functions defined inside of a class.
  3. use class keyword with {}
  4. An object is a variable using that class type.
1 Like

1. Why do we need classes, why can’t we just use structs?
We can use structs for everything we do with classes, however for good programming practice, we should use struct for data-only structures and classes for for objects that have both data and member functions.

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

3. How would you define a class?

class Awesome {         (Always with capital letter)
    public: (or private:)

        variables, member functions and nested types here

}

4. 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?
    With classes you can group data and functions together

  2. What are methods?
    function defined inside a class

  3. How would you define a class?
    object-oriented programming, or program defined types

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

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

advantage of using classes is that they support inheritance and polymorphism, which allows for more flexible and modular code. Structs, on the other hand, do not support inheritance or polymorphism and are limited to simple data structures.
Class can be used for a huge amount of data,. Strict is used for smaller amounts of data.

  1. What are methods?

Functions that belong to a class type are called member functions. Example is day, and month for the struct or class " date ".

  1. How would you define a class?

A class is a group of objects that share common properties and behavior.
A class is like a blueprint for an object.

  1. What is an object?

An Object is an instance of a class. Class is a blueprint or template from which objects are created.
Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc.