object oriented programming (oop)
please give me full answer thanks
Learning Objective:
1. Classes and Object Creation
2. Access specifies. How to use it?
3. What is Constructor.
4. Practical walk through.
5. Exercises
Classes and Objects
A class is like a blueprint of data member and functions and object is an instance of class.
For example, let’s say we have a class Car which has data members (variables) such as
speed, weight, price and functions such as gearChange(), slowDown(), brake() etc. Now let’s
say I create a object of this class named FordFigo which uses these data members and
functions and give them its own values. Similarly, we can create as many objects as we want
using the blueprint(class)
class Car
{
//Data members
char name[20];
int speed;
int weight;
public:
//Functions void brake(){
}
void slowDown(){
}
};
int main()
{
//ford is an object
Car ford;
}
Access specifies (Private, Public and protected)
The body of the class contains two unfamiliar keywords: private and public. A key feature of
OOP is data hiding; means that data is concealed within a class, so that it cannot be accessed
mistakenly by functions outside the class. The primary mechanism of hiding data is to put it in a
class and make it private. Private data or functions can only be accessed from within the class.
Public data or functions, on the other hand, are accessible from outside the class.
Usually the data within a class is private and the functions are public. This is a result of how
classes are used. The data is hidden so it will be safe from accidental manipulation, while
functions that operate on the data are public so they can be accessed from outside the class.
However, there is no rule that data must be private and functions public; in some circumstances
you may find you’ll need to use private functions and public data.
Constructor
A class constructor is a special member function of a class that is executed whenever we create
new objects of that class.
A constructor will have exact same name as the class and it does not have any return type at all,
not even void. Constructors can be very useful for setting initial values for certain member
variables.
Learning Objective:
1. Classes and Object Creation
2. Access specifies. How to use it?
3. What is Constructor.
4. Practical walk through.
5. Exercises
Classes and Objects
A class is like a blueprint of data member and functions and object is an instance of class.
For example, let’s say we have a class Car which has data members (variables) such as
speed, weight, price and functions such as gearChange(), slowDown(), brake() etc. Now let’s
say I create a object of this class named FordFigo which uses these data members and
functions and give them its own values. Similarly, we can create as many objects as we want
using the blueprint(class)
class Car
{
//Data members
char name[20];
int speed;
int weight;
public:
//Functions void brake(){
}
void slowDown(){
}
};
int main()
{
//ford is an object
Car ford;
}
Access specifies (Private, Public and protected)
The body of the class contains two unfamiliar keywords: private and public. A key feature of
OOP is data hiding; means that data is concealed within a class, so that it cannot be accessed
mistakenly by functions outside the class. The primary mechanism of hiding data is to put it in a
class and make it private. Private data or functions can only be accessed from within the class.
Public data or functions, on the other hand, are accessible from outside the class.
Usually the data within a class is private and the functions are public. This is a result of how
classes are used. The data is hidden so it will be safe from accidental manipulation, while
functions that operate on the data are public so they can be accessed from outside the class.
However, there is no rule that data must be private and functions public; in some circumstances
you may find you’ll need to use private functions and public data.
Constructor
A class constructor is a special member function of a class that is executed whenever we create
new objects of that class.
A constructor will have exact same name as the class and it does not have any return type at all,
not even void. Constructors can be very useful for setting initial values for certain member
variables.
Extracted text: Main Code: Source.cpp main.cpp x Header.h (Global Scope) Q#include "Header.h" #include "iostream" #include "string" using namespace std; Bint main() { student stu; stu.setName ("Awais Nawaz"); stu.setRollno (123); cout <« stu.getname="" ()="" «="" endl;;="">«><>
Extracted text: Example: Header Code: main.cpp Source.cpp student Header.h + X E#include "iostream" #include "string" using namespace std; class student { private: string name; int rollno; public: student(); void setName(string); string getName (); void setRollno(int); int getRollno(); }; Server Explorer Toolbox