1. Classes and Object Creation 2. Access specifies. How to use it? 3. What is Constructor. 4. Practical walk through. 5. Exercises

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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.

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;;
cout<<stu.getRollno()<<endl;
system("pause");
return 0;
Server Explorer Toolbox
Transcribed Image 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;; cout<<stu.getRollno()<<endl; system("pause"); return 0; Server Explorer Toolbox
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
Transcribed Image 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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY