-
Classes allow us to define member functions as well as member variables in a unit. They also allow us to encapsulate the state.
-
Methods are the member functions defined in a class. We can access them like we access member variables with the ‘.’ operator.
-
A class is a blueprint for an object. Objects of a class inherit its members. Example definition:
class ThisClass{
public:
m_var{};
classfunction(){
func body
}
}; -
An object is an instance of a class. It has associated variables and functions.
- We need classes because they are better suited for functions. Structs can also have methods but there might be some issues with memory allocation, so it is much safer to use classes, whenever we need both member functions and some data in a single unit.
- Methods are functions defined inside of a class. They are also know as member functions.
- Classes are defined with keyword
class
. Example:
class SomeClass
{
public:
int m_someValue{};
string m_otherValue{};
void someFunction()
{
//doing something useful
}
};
- An object is an instance of a class. Class is something like a blueprint for an actual object.
1. Why do we need classes, why can’t we just use structs?
Classes are more flexible in a number of areas. They form the basis for object-oriented programming, can be public or private, and use member functions without memory allocation issues, etc.
2. What are methods?
Methods are member functions that are defined either inside or outside the class.
3. How would you define a class?
class ClassName
{
public:
dtype mem_var{};
void memfunct(dtype var)
{
mem_var = var
};
};
4. What is an object?
It is an instance of assigned variables to a classes, e.g. ClassName objectname {var1, var2, var3};
1. Why do we need classes, why can’t we just use structs?
We use classes as a blueprint to create objects that can have embedding functions (methods). Structs are typically used to hold data only.
2. What are methods?
Methods are functions within a class.
3. How would you define a class?
Like this: class ThisIsMyClass … Classes are blueprints that enable us to create any number of objects using this blueprint (template). Classes are the backbone of object oriented programming.
4. What is an object?
An object is an instance of a class. In other words, it is an instantiated version of a blueprint (class).
- Why do we need classes, why can’t we just use structs?
structs have certain deficiencies when it comes to containing member methods. structs are generally used to contain data types only, not functions.
- What are methods?
Methods are functions defined within classes. They are members of the class, and they carry out instructions relevant to the object class.
- How would you define a class?
Classes are define with the keyword “class”.
EX:
class Name {
//access specifiers
// variables
// methods
};
- What is an object?
An object is an instance of a class and it inherits all the properties off the class.
- classes are useful when you want to include functions and private or protected objects in your struct like structure
- methods are the functions inside classes
- class classname{ public: objects; private: objects; functions};
- a piece of memory to store values, but looking at it from OOP perspective it also combines this aspect with properties and behaviors
-
We need classes to provide functions that work with our data. We can’t just use structs because they can hold only data.
-
Methods are the functions defined inside a class. They are also called member functions.
-
I would define a class as similar to a struct. The only difference is that a class is introduced by the keyword
public
. -
An object is the result of a method’s call.
Please correct me wherever I might have answered incorrectly. Thank you!
Classes don’t have public or private keyword in the definition, but you can declare variable and methods inside a class as public or private and also the class inheritance.
In the context of the homework an object is an instance of a class. Technically an object can be any declared…object i.e. variable, struct
One should consider structs as only for storing data. If one wants to add functions one creates a Class instead.
Methods are functions that are defined within a Class.
class MyClass {
}
It is a rule to always start class names with a capital letter.
An object is an instance of a class.
- We not only want to just hold data but we want a way to provide functions that work with the data as well
- Functions defined inside of a class
- class DateClass
{
public:
int m_year{};
int m_month{};
int m_day{}; - A variable of a type class
1 This is to make programs easier to understand/debug. The convension is structs contain only variables, efen if they can contain other objects. Classes may (by convension) also contain functions (metods).
2 A function defined within the class.
3 The name of the class (must) begin with a capital letter. The variables in a class are named as in struct, but will begin with “m_” and prior to the variables we use the keyword public just to make the variables exist outside the definition as well.
4 An object is something with the structure of the class. I.e. the class defines the objects.
- in classes you can incorporate functions additionally to a struct
- functions defined inside a class (member functions or methods)
- class Classname { …};
- class definitions is the blueprint of an object. an object can be anything (company, person, market etc)
-
Why do we need classes, why can’t we just use structs?
Structs only hold data (they only hold variables as members). Classes can hold functions as well, and are used for object orientated programming. The data and data-related functions can be grouped together in a Class. -
What are methods?
These are member functions, ie those functions that are defined inside of a class. They can be accessed in a similar way to the member variables, by using the ‘.’ operator. -
How would you define a class?
Using the keywordsclass
, andpublic
and a naming convention onm_variableName
for the variable members. For example:
class ExampleClass
{
public:
int m_variable1{};
string m_variable2{};
int m_variable3{};
void exampleFunction()
{
//do something in here
}
}; //needs the semicolon
-
What is an object?
An object of a class is where a variable of that class type has been defined (i.e had values assigned). It is an instantiated class object.
QUIZ 1a
#include <iostream>
using namespace std;
class IntPair
{
public:
int m_value1{}; // member variable in Class, use the m_ to indicate member variable
int m_value2{};
void set(int first, int second) // defines a member function called set
{
m_value1 = first;
m_value2 = second;
}
void print() // defines a member function named print()
{
cout << "Pair(" <<m_value1 << ',' << m_value2 << ")\n"; //\n puts a line break in
}
};
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;
}
QUIZ 1b - why use a class instead of a struct?
Because it has member variables and functions. Structs only have variables.
This will not work, you would have to declare a constructor or overload an operator
IntPair() {} // In case you overload the constructor you must also define the default one or p1 will throw an error
IntPair(int first, int second) { // To call a constructor you must then initialize the class with parameters IntPair p1(1, 2)
set(first, second);
}
IntPair operator=(int args[2]) { // This will work with your current example with curly brackets
set(args[0], args[1]);
return *this;
}
Thanks for explaining!
I’m a bit confused by the Quiz… at the point I did it, I hadn’t come across constructors.
I wrote my code, tested it and the output was what was expected (I get that it may look right but may not be calculating right etc). I checked the code against the solution on the website and it was very similar (variable names differed etc).
The int main () body was from the question, I wrote the class.
When you get there constructors are certainly the way to go!
I’ve had to go and read about what operator overloading is… I may need to get more coffee and read it again, but I think it is fundamentally to allow a function of the same name to work with different data types, so a function called add works with integers, could also work with strings…
Probably need more coffee so I could be totally wrong on this!
Many thanks
Operator overloading is when you overload the default behavior of an operator (+, -, *, /, =) and give it custom functionality. One of my favorite features in C++
In the case above we overloaded the behavior of assignment (=
) so that you are able to assign values to your class as you intended.
Thank you, that has helped me to understand it!
I need to make time to really understand that part, I can see how useful it would be. Many years ago I did C… bit rusty to be honest! :-
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.
What are methods?
Functions defined inside of a class are called member functions (or sometimes methods)
How would you define a class?
Class (and struct) definitions are like a blueprint – they describe what the resulting object will look like, but they do not actually create the object.
What is an object?
It is what a Class describes.
An object is an instance created with the blueprint you described in previous answer.
1.)Classes allow us to store data and functions while structs are generally recommended only for storing data types.
2.) Methods is another term for member functions and member functions are functions defined in a class.
3.) A class is a simply a user defined type. In object orientated programming we need our types to not only hold data but also hold functions that can work the data inside our specified class.
4.)In c++, an object is defined as a “piece of memory that can be used to store values”.