CS-Object Oriented Programming ++with C: Lecture 2: Introduction
CS-Object Oriented Programming ++with C: Lecture 2: Introduction
++with C
1
Lecture 2: Introduction
2 Contents of course
Procedural Approach:
Real-world Programming
Attributes Data
- eye color (for people) They have a certain specific values.
- horsepower (for cars)
Behavior Functions
A Behavior is something a real-world you call a function to do something
object does in response to some (display the inventory, for
stimulus. If you apply the brakes in a example) and it does it.
car, it will generally stop.
Object Oriented Programming
7
Object-Oriented Approach
College Environment
Student
Teache
Course
r
Section Office
Object Oriented Programming
9
What kinds of things become objects in object-oriented programs?
Objects in super market program
Product
Cashier Customer
Cart Bagger
Object Oriented Programming
10
Objects is comprised of ?
Objects: comprised of associated DATA and OPERATION (methods)
which manipulate that data.
Object
Data
()Operations
? Objects is comprised of
11
Product
Data
Product_name-1
Product_code-2
Price-3
Producer-4
()Operations
()Modify price-1
()Get Product Name-2
()Get Product Price-3
? Objects is comprised of
12
Car
Data
Model-1
Fuel_capacity-2
No_of_doors-3
Color-4
Shape-5
()Operations
() Change Color-1
()Get Car Info-2
()..……-3
13 Classes And Objects
- When the program is running, it can use the class to create as many
objects of a specific type as needed.
- Each object that is created from a class is called instance of the
class.
.How to create a class ,step-by-step
16
class MyClass // The class
Operations()
void readdata() //member function to read data
1. setdata()
2. readdata() { cin>> myString ;}
3. showdata()
void showdata() //member function to display data
{ cout << “My number is “ << myNum << endl;
cout << “My string is “ << myString << endl; } };
17 Classes And Objects
Class MyClass
int main()
Data: {
1. myNum
Obj1 MyClass Obj1; // Create an object of class MyClass
2. myString
Data: // Access attributes and set values
Operations()1. myNum Obj1.myNum = 15;
2. myString Obj1.myString = "Some text";
1. setdata()
2. readdata()
Operations() // Print attribute values
3. showdata() cout << Obj1.myNum << "\n";
1. setdata()
cout << Obj1.myString;
2. readdata()
return 0;
3. showdata() }
Definition Or
18 specifier
Classes And Objects
class MyClass // The class
int main()
{ private: // Access specifier
int myNum; // Attribute (int variable) { // Create an object of class MyClass
string myString; // Attribute (string variable)
MyClass Obj1,Obj2;
public:
Access specifier
void setdata(int d) //member function to set data Creating or instantiating
//call member function to set data
{myNum = d; } Obj1.setdata(1066);
void readdata() //member function to read data Obj1.readdata();
Obj2.setdata(1000);
{ cin>> myString ;}
Obj2.readdata();
void showdata() //member function to display data //call member function to display data
{ cout << “My number is “ << myNum << endl;
Obj1.showdata();
cout << “My string is “ << myString << endl; } }; Obj2.showdata();
return 0; Class member
} access operator
19 Access Modifier (Private and Public data)
Access modifier (or access specifier)is a c++ keyword that indicates
.how a data or operation can be accessed
▪ Public Data
hiding
Public data or functions are accessible from outside the class.
- when the public access modifier is applied to a class member, the
member can be accessed by code inside the class or outside.
▪ Private:
Private data or functions can only be accessed from within the class.
-when the private access modifier is applied to a class member, the
member cannot be accessed by code outside the class.The member can
be accessed only by methods that are members of the same class.
20 Data Hiding
•An object hides its internal, private fields from code that is outside the
class that the object is an instance of.
•Only the class's methods may directly access and make changes to the
object’s internal data.
•Code outside the class must use the class's public methods to operate
on an object's private fields.
Data hiding is important because classes are typically used as
components in large software systems, involving a team of
programmers.
Simple Example to create object from class car
21
A Car object will have the following fields:
Car
class Car
{ Data:
private: 1. Name
string name; 2. Color
string color; 3. Model
int model;
public: Operations()
void setdata(string n , string c, int m) //member function to set data 1. setdata()
2. showdata()
{ name = n;
color = c;
model = m; }
void showdata() //member function to display data
{ cout << "The name of car is " << name << endl;
cout << "The color of car is " << color << endl;
cout << "The model of car is " << model << endl;}};
Simple Example to create object from class car
22
A Car object will have the following fields:
()int main
}
Car c1; // Create an object of class Car
c1.setdata("Toyota","Black",2022); //call member function to set data
c1.showdata(); //call member function to display data
;return 0
}
()int main
}
Car c1,c2; // Create two object of class Car
c1.setdata("Toyota","Black",2022); //call member function to set data
c1.showdata(); //call member function to display data
c2.setdata(“Kia",“blue",2021);
c2.showdata();
;return 0
{