Classes in C++ - Reading Assignment

Why do we need classes, why can’t we just use structs?
we need classes when we have to group not only variables but functions, in only one variable.
.What are methods?
methods are functions inside a class
How would you define a class?
Is basically the same for a struct with the keyword public(or private i suppose for now)inside the class
class ClassName {
public:
// variables or/ and functions(methods)
};

What is an object?
Is an instance of the class.
class Example {
public:
int x;
};

Example integer = {1}// here integer is an instance of the class Example.

Excuse me I have here a little question about a struct below?

struct Example {
int x;
};

Example integer = {1}// here integer is an instance of the struct and an object?

1 Like

It can only be one or the other. This would not work in actual code because you can’t have two definitions with the same name. :slight_smile:

2 Likes
  1. " The class keyword lets us create a custom type in C++ that can contain both member variables and member functions." - https://www.learncpp.com/cpp-tutorial/82-classes-and-class-members/
  2. Methods are Functions defined inside of a class.
  3. A class defines what the resulting object will look like or be formed of.
  4. Objects are used and created in order to call upon functions.
1 Like
  1. Why do we need classes, why can’t we just use structs?

Structs don’t allow functions. Classes are more powerful.

  1. What are methods?

Functions inside a class ();

  1. How would you define a class?

class {functions;
objects;
};
4. What is an object?

An instance of a class.

1 Like
  1. Structs cannot contain functions while classes can.

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

  3. class className
    {
    public:
    //member variables
    //member functions or methods
    };

  4. A class type variable is called an object.

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

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

  3. How would you define a class?
    A class is a user-defined type or data structure declared with keyword class that has data and functions

  4. What is an object?
    Is an instance of a Class, when the Class is instantiated an object is created and memory allocated.

1 Like

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

A1. Classes can also contain functions (methods) whereas structs can only contain variables/data

Q2. What are methods?

A2. Class member functions

Q3. How would you define a class?

A3. Syntax:

class newClass {

...

};

Q4. What is an object?

A4. An object is an instance of a class - i. e. the latter is like a blueprint which describes what the resulting object will look like and behave

1 Like

Alko89 - I’m looking forward to the day when I can understand C++. Thank you for your encouragement with my homework assignments

Q: Why do we need classes, why can’t we just use structs?
A: Because we often want to not only hold data (structs), but provide functions that work with the data as well (classes).

Q: What are methods?
A: Methods are functions defined in the classes.

Q: How would you define a class?
A: class ClassName {
public:
};

Q: What is an object?
A: An object is an instance of a class.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Because a class is an structure that can do things , can have functions inside , while an structure is just a group of attributes together that define a common entity.
    Another characteristic is that members on. the class are private by default , while methods are public. So security is higher on the class

  2. What are methods?
    Functions ,actions that the class can perform itself on its own as an object.

  3. How would you define a class?
    class Name{
    variables

    methods

    }

  4. What is an object?
    an object is an instance of a class, lets say when the class is initialized and allocated in memory

1 Like
  1. When one wants 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. Functions defined inside of a class are called member functions (or sometimes methods).
  3. class ClassName{ ... };
  4. A class describes how an object will look like, but does not actually create an object. An object is a created instance.
1 Like

Classes work well with member functions.

Methods are functions defined inside of a class.

With the keyword class.

class Example
{
public:
    string m_name{};
    int m_age{};
};

An object is a variable of type class.

1 Like

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

Answer: Classes provides functions that works out with data.

What are methods?

Answer: Methods are functions that belongs to a class.

How would you define a class?

Answer: class ClassName{

Public:

//Public members and methods

Private:

//Private members and methods }

What is an object?

Answer: An instance of a class

1 Like

Why do we need classes, why can’t we just use structs? The only difference is default visibility and struct is already defined in C. Structs should be used to represent a single value because structs are value types, like a number. … This is important because structs are value types which are copied by value.

What are methods? A method in object-oriented programming (OOP) is a procedure associated with a message and an object.

How would you define a class? A class is define d in C+ + using keyword clas s followed by the name of clas s .

What is an object? An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.

1 Like
  1. We need classes for 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 methods.

  3. A class is defined just like the struct but with a public keyword. For example:
    class DateClass
    {
    public:
    int m_year{};
    int m_month{};
    int m_day{};
    };

  4. In traditional programming, an object is a piece of memory to store values. And that’s it. In object-oriented programming, an “object” implies that it is both an object in the traditional programming sense, and that it combines both properties and behaviors.

2 Likes
  • Structs only hold data. We need classes because they allow us to hold data and use functions that work with the data being held.

  • Methods are member functions, which are functions defined inside a class

  • You define a class with the class keyword then the name of the class and body in curly braces.

class ClassName {
	Access specifier; 	//private, public, or protected
	Data members;	//variables to be used
	Member Functions(){
      }		//methods to access data members
};			//class name must end wth semicolon
  • An object is an instance of a class. When a class is instantiated, memory is allocated.
1 Like

1. Why do we need classes, why can’t we just use structs?
Because classes helps to take all the properties of an object in a program, after that it combine them into a single interface, then use that interface wherever there is a need.

2. What are methods?
They are code reusability, which saves computational time and makes the code in a good structure and clean.

3. How would you define a class?
It is a mechanism of binding data members and associating methods in a single unit.

4. What is an object?
It is an instance of a class or a struct but it can also be an instance of a fundamental type.

1 Like

Classes help with code reusability, methods are functions defined in a class. :slight_smile:

1 Like
  1. Because 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. Structs only hold data while Classes not just only hold data but also can provide functions.

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

  3. class ClassName
    {
    public:
    //member variables ex: int m_year
    //member functions ex: void print {…}
    };

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

2 Likes

Reading Assignment: Classes in C++ (Answers)

  1. Because often we want our type not only to hold data, but provide functions that work with the data as well.
  2. Functions defined inside a class are called member functions( or Methods).
  3. Class lets us create a custom type that contains both member variables and member functions.
    4.An instance of a class or struct.
1 Like