ITE 6102 - Computer Programming 1 - VC - Sept 11
ITE 6102 - Computer Programming 1 - VC - Sept 11
COMPUTER
PROGRAMMING 1
VIRTUAL CLASS – SEPTEMBER 11, 2020
• C++ Arrays
• C++ Functions
• C++ Functions Parameters
• C++ Functions Overloading
C++ OOP
• Procedural programming is about writing procedures or functions that perform operations on the
data, while object-oriented programming is about creating objects that contain both data and
functions.
• Object-oriented programming has several advantages over procedural programming:
• OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to
maintain, modify and debug
• OOP makes it possible to create full reusable applications with less code and shorter development
time
C++ CLASSES AND OBJECTS
• Everything in C++ is associated with classes and objects, along with its attributes and methods. For
example: in real life, a car is an object. The car has attributes, such as weight and color, and
methods, such as drive and brake.
• Attributes and methods are basically variables and functions that belongs to the class. These are
often referred to as "class members".
• A class is a user-defined data type that we can use in our program, and it works as an object
constructor, or a "blueprint" for creating objects.
CREATING A CLASS
Example:
Create a class called "MyClass":
• In C++, an object is created from a class. We have already created the class named
MyClass, so now we can use this to create objects.
• To create an object of MyClass, specify the class name, followed by the object name.
• To access the class attributes (myNum and myString), use the dot syntax (.) on the
object
EXAMPLE
#include <iostream>
#include <string>
};
EXAMPLE (CONT.)
int main() {
myObj.myNum = 15;
return 0;
}
C++ CLASS METHODS
}
OUTPUT:
};
Hello World!
EXAMPLE (OUTSIDE CLASS)
};
}
C++ CONSTRUCTORS
• Constructor is a special member function of a class that initializes the object of the
class. Constructor name is same as class name and it doesn’t have a return type.
• To create a constructor, use the same name as the class, followed by parentheses ().
EXAMPLE 1
using namespace std; MyClass myObj; // Create an object of MyClass (this will call the constructor)
MyClass() { // Constructor
} OUTPUT:
};
Hello World!
EXAMPLE 2
#include <iostream>
class constructorDemo {
public:
int num;
char ch;
constructorDemo() {
};
EXAMPLE 2 (CONT.)
int main() {
constructorDemo obj;
cout<<"num: "<<obj.num<<endl;
cout<<"ch: "<<obj.ch;
return 0;
}
OUTPUT:
num: 100
ch: A
CONSTRUCTOR PARAMETERS
• Constructors can also take parameters (just like regular functions), which can be useful
for setting initial values for attributes.
EXAMPLE
#include <iostream>
brand = x;
model = y;
year = z;
EXAMPLE (CONT.)
}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
The public keyword is an access specifier. Access specifiers define how the members (attributes and
methods) of a class can be accessed. In the example given, the members are public - which means
that they can be accessed and modified from outside the code.
However, what if we want members to be private and hidden from the outside world?
2. private - members cannot be accessed (or viewed) from outside the class
3. protected - members cannot be accessed from outside the class, however, they can be accessed
in inherited classes.
EXAMPLE
#include <iostream>
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (x is public)
return 0;
} OUTPUT:
• The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from
users. To achieve this, you must declare class variables/attributes as private (cannot be
accessed from outside the class). If you want others to read or modify the value of a
private member, you can provide public get and set methods.
#include <iostream>
class Employee {
private:
int salary;
public:
void setSalary(int s) {
salary = s;
}
EXAMPLE (CONT.)
int getSalary() {
return salary;
};
int main() {
Employee myObj;
myObj.setSalary(50000); OUTPUT:
}
WHY ENCAPSULATION?
• It is considered good practice to declare your class attributes as private (as often as you
can). Encapsulation ensures better control of your data, because you (or others) can
change one part of the code without affecting other parts
• Increased security of data
C++ INHERITANCE
• In C++, it is possible to inherit attributes and methods from one class to another. We
group the "inheritance concept" into two categories:
• derived class (child) - the class that inherits from another class
• base class (parent) - the class being inherited from
• To inherit from a class, use the : symbol.
SYNTAX OF INHERITANCE
class parent_class
{
//Body of parent class
};
class child_class : access_modifier parent_class
{
//Body of child class
};
ADVANTAGES OF USING INHERITANCE IN C++
PROGRAMMING
• Code Reusability
• Readability
EXAMPLE
class Animal {
// eat() function
// sleep() function
};
• Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.
• Like we specified in the previous slides; Inheritance lets us inherit attributes and
methods from another class. Polymorphism uses those methods to perform different
tasks.This allows us to perform a single action in different ways.
TYPES OF C++ POLYMORPHISM
• Compile time Polymorphism – This is also known as static (or early) binding.
#include <iostream>
class Polygon {
protected:
public:
{ width=a; height=b; }
};
EXAMPLE (CONT.)
public:
int area()
{ return width*height; }
};
public:
int area()
{ return width*height/2; }
};
EXAMPLE (CONT.)
int main () {
Rectangle rect;
Triangle trgl;
ppoly1->set_values (4,5);
}
EXAMPLE 2
#include <iostream>
using namespace std;
int main() {
class MyClass
EXERCISE NO. 2
MyClass myObj;
EXERCISE NO. 3
• Use an access specifier to make members of MyClass accessible from outside the class.
class MyClass {
:
int myNum;
};
EXERCISE NO. 3 (ANSWER)
class MyClass {
public:
int myNum;
};
EXERCISE NO. 4
• Create an object of MyClass called myObj, and use it to set the value of myNum to 15.
class MyClass {
public:
int myNum;
};
int main() {
;
.;
cout << myObj.myNum;
return 0;
}
EXERCISE NO. 4 (ANSWER)
class MyClass {
public:
int myNum;
};
int main() {
MyClass myObj;
myObj.myNum = 15;
return 0;
}
EXERCISE NO. 5
class MyClass {
public:
MyClass() {
};
int main() {
MyClass myObj;
return 0;
}
Thank you for listening.
If you have any questions please send a message thru LMS chat box or email me
at [email protected]