1.we can use functions with the class, but with structs it only stores data
2.funtions in the class
- class NAME{};
4.when a class is called
1.we can use functions with the class, but with structs it only stores data
2.funtions in the class
4.when a class is called
For matters of convenience, classes are better to implement that structs when you have to store data and functions into an objects. Structs are better to use when youāre only dealing with data.
Methods are functions defined within a class.
class Thing {}
An object is an instance of a class
Why do we need classes, why canāt we just use structs?
We need class for defining objects that require both data and functions to be bundled together. Structs are best used for for data-only structures.
What are methods?
Functions defined inside of a class.
How would you define a class?
Like a blueprint. They describe what the resulting object will look like.
What is an object?
An object is an instance of a class.
Classes are used for objects that hold both data and functions, while structs are used for data-only structures.
Methods, or member functions, are functions declared inside of a class.
Classes are commands that define new user-defined types. Classes can hold both data and functions.
The object is the actual entity inside the blueprint, which is the class.
We use classes to hold data and also functions that work with the data, whereas structs are used to hold data.
Class functions are called methods or member functions.
3.Class functions may be defined inside or outside of the class definition and are accessed using the member selector operator, (.
) e.g.
class Person{
public:
string name;
int age;
void printInfo(){
cout<<"Person's name is "<<name<<endl;
cout<<"Person's age is "<<age<<" years"<<endl;
}
};
int main()
{
Person human={"Human",99};
human.printInfo();
return 0;
}
class MyClass{
public:
//member variables, methods
private:
//member variables, methods
};
class MyClass {
...
};
Questions:
. Classes allow for functions which can work well with data and not just store it.
. Methods are functions defined inside of a class.
. Class nameOfClass
{
};
. An object is a type of class.
Classes provide the option of having methods or functions inside them that can operate on external data passed into them or on the member variables of the class they are a part of.
Methods are functions that belong to a class. They can only be called on objects of a particular class only after that object has been initialized and only using the . operator
A class is a data type defined by the programmer containing several member variables that determine the state of the object of the class. For example a class representing a vehicle could have member variables representing the make, model, and year along with possibly more member variables that determine the state of any car object.
The class also contains methods, which are functions defined inside the class that can operate on data external to the class as well as on the internal data i.e. the member variables to perform some associated action.
An object is a specific instance of the class i.e. a class that exists in memory and has been initialized such that its members have specific values and its methods can be called via the dot operator (.) and the object name which the user chose.
Quiz time
Question #1
a) Create a class called IntPair that holds two integers. This class should have two member variables to hold the integers. You should also create two member functions: one named āsetā that will let you
assign values to the integers, and one named āprintā that will print the values of the variables.
The following main function should execute:
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;
}
and produce the output:
Pair(1, 1)
Pair(2, 2)
Solution:
#include
class IntPair
{
public:
int m_a{};
int m_b{};
int set(int a, int b)
{
m_a=a;
m_b=b;
return 0;
}
int print()
{
std::cout<<"Pair("<<m_a<<", "<<m_b<<")\n";
return 0;
}
};
int main()
{
IntPair p1;
p1.set(1, 1);
IntPair p2 { 2, 2 };
p1.print();
p2.print();
return 0;
}
class thisClass{
public: ā¦ā¦ā¦.
ā¦ā¦ā¦ā¦;
private: ā¦ā¦ā¦ā¦ā¦..;
}
Structs are good for data-only structures but classes are better suited for objects that contain data and functions.
Methods are member functions. They are functions that are defined inside of a class.
Classes are defined the same way structs are defined. The only major difference is that classes will have the public or private keywords to specify access.
When you define a class, you are creating a blueprint that you can later use to build objects. You create objects by specifying a class and giving them data members and methods.
1. Why do we need classes, why canāt we just use structs?
Structs are used to store data, while classes have functions and data.
2. What are methods?
functions that are members in a class.
3. How would you define a class?
class SampleClass { define variables / methods};
4. What is an object?
An object is a instance of a class.
1. Why do we need classes, why canāt we just use structs?
We can employ member functions in classes as well as data.
2. What are methods?
Functions defined inside a class (member functions)
3. How would you define a class?
A data structure which can include both variables and member functions
4. What is an object?
A variable (instance) of a class type
1. Why do we need classes, why canāt we just use structs?
Because in some situations in object-oriented programming, programmers do not want types to only hold data, but to hold functioins that can operate on the data. This is where classes come in. the class keyword creates a new user-defined data-type called class.
2. What are methods?
Functions defined within a class (member functions)
3. How would you define a class?
The same way as you define a struct, though with the addition of the keyword āpublicā or āprivateā to indicate access.
class ExampleClass
{
public: // defines accessability
int value {};
void function ()
{
executable code
}
};
4. What is an object?
An instance of a class, so if I take the above ExampleClass and declare it with values, that is an ExampleClass object.
class theClass {stuff you want in theClass}
1. Why do we need classes, why canāt we just use structs?
when they only hold variables they are exactly the same, with only one difference: classes need to specify if the variables are public or private (scope). But classes can also contain functions as members, and these are called methods.
after reading the whole lesson⦠turns out I was wrong:
āIn C, structs can only hold data, and do not have associated member functions. In C++, after designing classes (using the class keyword), Bjarne Stroustrup spent some amount of time considering whether structs (which were inherited from C) should be granted the ability to have member functions. Upon consideration, he determined that they should, in part to have a unified ruleset for both. So although we wrote the above programs using the class keyword, we could have used the struct keyword instead.ā
Use the struct keyword for data-only structures. Use the class keyword for objects that have both data and functions.
2. What are methods?
Methods are members of a class, but the member type is a function.
3. How would you define a class?
class Student {
public :
string name{};
int age{};
string email{};
string courses[]{c++, bitcoin101};
void printStudentInfo() {
std::cout<<"Student "<<name<<" is "<<age<<" years old, his email is "<<email<<" and has taken the following courses:"<<endl<<courses[0]<<endl<<courses[1]<<endl;
}
private :
float grades[]{6.5, 5.9};
};
4. What is an object?
Class is mere a blueprint or a template. No storage is assigned when we define a class. Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
Each object has different data variables. Objects are initialized using special class functions called Constructors . And whenever the object is out of its scope, another special class member function called Destructor is called, to release the memory reserved by the object.