Work@tech OOPS Bascis in C++
Work@tech OOPS Bascis in C++
Let's discuss this specificity at a later stage. At this stage, let's give a
name to the type that we are talking about.
These types are known as classes. A class is a definition or a
blueprint of objects of the same type.
We all are objects created from the blueprint of a Human class. Your
Lenovo laptop is created from the blueprint of a Laptop class.
Questions: 4
Start Quiz
Let's say if we were to create mobile phones, what are some of the
different attributes that a phone might have.
Brand
Model
RAM
Storage
Processor
Screen Size
Operating System
Color
Weight
Let's also take a look at the different functions that the phone needs to
allow the user to do:
Dial Call
Receive Call
Send Message
Open App
Now the attributes and the functions together make up the Mobile
class (blueprint for any mobile phone).
Questions: 3
Start Quiz
Examples:
The data and functions within a class are called members of the class.
There is no way to interact directly with a class as they are logical
entities. We can interact with objects as they are real or abstract
entities either present in the real world or in the digital world.
In OOP, data and the functions that operate on them are bound
together in classes. The primary focus of OOP languages is data
(objects).
C++ which is an object-oriented programming language was mainly
created to allow object orientation to the C language.
Abstraction
When we are using our mobile phone, we know how to change the
volume or click a photo or lock/unlock the phone. But what we don't
know is how it internally works. The internal implementation is
abstracted out from us.
While using this website and running code on the IDE, you are
actually interacting with an abstraction layer where you do not actually
know or have to deal with how the code is executed internally. You
engage with the website just through a button.
While learning to code, we actually created many abstraction layers
without actually knowing that we are creating abstractions.
These are few of the scenarios where you must have used abstraction
while learning to code:
Questions: 2
Start Quiz
Similar to how int, float, char, etc are in-built data types used to
represent different types of data, a class is a user-defined data type
used to represent some type of data.
Unlike primitive data types like int, float, etc, classes can have
different attributes to represent the data and certain functions to
operate on that data.
The attributes and functions of a class are also known as the class
members.
Example
The above diagram denotes the class Phone with its attributes and
functions. This type of diagram is known as UML (Unified Modeling
Language) Class Diagram. It is used to visualize a class and its
relationships.
Note that we have picked up a limited set of attributes and functions of
a phone for the sake of simplicity.
Questions: 4
Start Quiz
Now let's look at how to define the Phone class through a C++ code.
class Phone {
public:
string brand;
string model;
int ram;
int storage;
};
Here, we are creating a class named Phone with all of the members
mentioned above.
Here, attributes are just like variables and class methods are just like
any other method/function. They are just bound together in a single
entity: the class.
Please note that we can access all the attributes of the class in the
class methods.
Example:
//Here dialCall method can access any of the class attributes (brand, model, etc)
void dialCall(string number) {
cout << "Calling " << number << " from " << brand << ":" << model << "\n";
}
class ClassName {
private:
data_type attribute_name;
data_type attribute_name;
.
.
.
return_type function_name( parameters ) {
//Some logic
}
.
.
.
public:
data_type attribute_name;
data_type attribute_name;
.
.
.
return_type function_name( parameters ) {
//Some logic
}
.
.
.
};
Here, we declare the class with:
Please do not forget to add the semicolon at the end of the class code
block. It is a common mistake that beginners make and end up getting
a compilation error.
Creating objects
#include <bits/stdc++.h>
using namespace std;
class Phone {
public:
string brand;
string model;
int ram;
int storage;
int main() {
// your code goes here
Phone my_iPhone_11;
Phone my_iPhone_xs;
Phone my_oneplus_7;
return 0;
Similar to how we use in-built data types (int, float, etc) to create
variables of those types, we do the same for an object (a variable of a
user-defined class) as well. We can create as many objects as we
want similar to how we can create any number of int variables.
int num;
Phone my_iPhone_11;
As you can see that we are not setting any value to the objects
created in the above code. Let's look at how to do it.
We can access class members using the dot (.) operator on the
object.
Example:
int main() {
Phone my_iPhone_11;
my_iPhone_11.brand = "Apple";
my_iPhone_11.model="iPhone 11";
my_iPhone_11.ram=4;
my_iPhone_11.storage=64;
cout << my_iPhone_11.brand << " " << my_iPhone_11.model << " " <<
my_iPhone_11.ram << " " << my_iPhone_11.storage << "\n";
my_iPhone_11.dialCall("9732130450");
my_iPhone_11.receiveCall("9732130450");
return 0;
}
Expected Output
Apple iPhone 11 4 64
Calling 9732130450 from Apple:iPhone 11
Receiving call from 9732130450 on Apple:iPhone 11
Apple iPhone XS 4 64
Calling 9732130450 from Apple:iPhone XS
Receiving call from 9732130450 on Apple:iPhone XS
OnePlus 7t 8 128
Calling 9732130450 from OnePlus:7t
#include <bits/stdc++.h>
class phone{
public:
string brand;
string model;
int ram;
int storage;
cout << "Calling " << number << " from " << brand << ":" << model << "\n";
cout << "Receiving call from " << number << " on " << brand << ":" <<
model << "\n";
}
};
int main() {
phone my_iphone_11;
my_iphone_11.brand = "Apple";
my_iphone_11.ram = 4;
my_iphone_11.storage = 64;
cout<<my_iphone_11.brand<<" "<<my_iphone_11.model<<"
"<<my_iphone_11.ram<<" "<<my_iphone_11.storage<<"\n";
my_iphone_11.dialCall("9732130450");
my_iphone_11.receiveCall("9732130450");
phone my_iphone_xs;
my_iphone_xs.brand = "Apple";
my_iphone_xs.ram = 4;
my_iphone_xs.storage = 64;
cout<<my_iphone_xs.brand<<" "<<my_iphone_xs.model<<"
"<<my_iphone_xs.ram<<" "<<my_iphone_xs.storage<<"\n";
my_iphone_xs.dialCall("9732130450");
my_iphone_xs.receiveCall("9732130450");
phone my_onePlus_7t;
my_onePlus_7t.brand = "OnePlus";
my_onePlus_7t.model = "7t";
my_onePlus_7t.ram = 8;
my_onePlus_7t.storage = 128;
cout<<my_onePlus_7t.brand<<" "<<my_onePlus_7t.model<<"
"<<my_onePlus_7t.ram<<" "<<my_onePlus_7t.storage<<"\n";
my_onePlus_7t.dialCall("9732130450");
my_onePlus_7t.receiveCall("9732130450");
return 0;
Constructor
Questions: 3
Start Quiz
Till now we have learned how to create an object and assign values to
object properties using the dot operator.
The constructor for the phone class would be defined like this:
class Phone {
public:
string brand;
string model;
int ram;
int storage;
Phone () {
brand = brandValue;
model = modelValue;
ram = ramValue;
storage = storageValue;
cout << "Calling " << number << " from " << brand << ":" << model << "\n";
cout << "Receiving call from " << number << " on " << brand << ":" <<
model << "\n";
}
};
Phone my_iPhone_11;
my_iPhone_11.brand = "Apple";
my_iPhone_11.model="iPhone 11";
my_iPhone_11.ram=4;
my_iPhone_11.storage=64;
Now let's look at the same example of object creation with the same
data through such a constructor.
or
my_iPhone_11.brand = "Apple";
my_iPhone_11.model="iPhone 11";
my_iPhone_11.ram=4;
my_iPhone_11.storage=64;
What is happening here is that when we are creating the object, the
default constructor is called internally and the object is created
through that constructor.
Phone my_iPhone_11;
is equivalent to
Let's write some code with all the concepts learned till now. Doing this
properly will help you retain these concepts and will make the
assessment easier for you to complete.
Create a class for the below UML Diagram.
Expected Output
#include <bits/stdc++.h>
class Phone{
public:
string brand;
string model;
int ram;
int storage;
Phone();
brand = brandV;
model = modelV;
ram = ramV;
storage = storageV;
}
cout << "Receiving call from " << number << " on " << brand << ":" <<
model << "\n";
};
int main() {
my_iphone11.dialCall("9732130450");
my_iphone11.receiveCall("9732130450");
my_iphone_xs.dialCall("9732130450");
my_iphone_xs.receiveCall("9732130450");
Phone my_onePlus_7t("OnePlust", "7t", 8, 128);
my_onePlus_7t.dialCall("9732130450");
my_onePlus_7t.receiveCall("9732130450");
return 0;
Examples
Example 1
Let's say that you want to know the number of objects (phones)
created from a particular class (Phone). To get that information we
would not want to create another object of that class, right? Since
every object has a different state and acts as different entities, we
won't be able to know this information from any of the objects anyway
as they don't know anything about one another.
We would want that information at the prototype level (in the class)
from which the object is created. This is possible to do using static
keyword. We will look at how to do it after all the examples.
Example 2
The issue here is that every object takes up some memory. If we want
to use these functions, we will have to create a separate object
everywhere we would want to use it thereby taking up a lot of memory.
This should not be required as these objects would not store any data
that we might need. This is also possible to do using static keyword.
We will look at how to do it after all the examples.
Example 3
Example:
class Phone {
.
.
};
//Function logic
Example:
class Calculator {
};
Questions: 1
Start Quiz
Note that static class members are not tied to any object and so
cannot access any of the non-static class members. Static class
members can however access other static class members.
Example:
class Phone {
public:
.
.
if (count/1000000000 > 0) {
} else {
return to_string(count);
};
Example:
Encapsulation
Introduction
Open your phone and open the calculator app. Try to visualize the
calculator app on your phone in terms of an object of class
CalculatorApp.
Encapsulation - Quiz 1
Questions: 3
Start Quiz
The app might not allow you to view specific attributes as well such as
the number of times you have opened the app. Your actions might
indirectly modify that attribute but the app cannot allow you to modify
that attribute directly.
Encapsulation - Quiz 2
Questions: 2
Start Quiz
Let's look at what public and private means that we saw in the class
definition template.
class ClassName {
private:
data_type attribute_name;
data_type attribute_name;
public:
data_type attribute_name;
data_type attribute_name;
.
.
//Some logic
};
public/private keywords used here are known as access specifiers or
access control modifiers.
Note that we can set any class member as private. It is not only for
attributes. We use private to restrict access/modification of class
members outside the class.
class Phone {
private:
string brand;
string model;
int ram;
int storage;
public:
Phone () {
model = modelValue;
ram = ramValue;
storage = storageValue;
cout << "Calling " << number << " from " << brand << ":" << model << "\n";
}
void receiveCall (string number) {
cout << "Receiving call from " << number << " on " << brand << ":" <<
model << "\n";
};
my_iPhone_11.brand = "Apple";
cout << my_iPhone_11.brand;
Whereas these things are still valid:
The UML diagram of the above code would look like this:
Note that instead of +, the attributes are now prefixed with -.
class Phone {
string brand;
string model;
int ram;
int storage;
public:
Phone () {
//any initialization logic
brand = brandValue;
model = modelValue;
ram = ramValue;
storage = storageValue;
}
void dialCall (string number) {
cout << "Calling " << number << " from " << brand << ":" << model << "\n";
cout << "Receiving call from " << number << " on " << brand << ":" <<
model << "\n";
};
Here, all the attributes are treated as private and all members after the
public keyword are treated as public.
Let's write some code.
Create a class for the below UML class diagram.
getName, getIcon, and getTheme should return the name, icon,
and theme respectively.
setTheme should take themeVal as a parameter and should
assign the passed value to theme. Look at how we do it in the
constructor.
Example: After calling calculator.setTheme("Dark");
theme should be set to "Dark"
plus, minus, multiply, divide should take two integers and return
the result based on the operation (+, -, *, /).
#include <bits/stdc++.h>
class CalculatorApp{
private:
string name;
string icon;
string theme;
public:
name = nameValue;
icon = iconValue;
theme = themeValue;
string getName()
{
return name;
string getIcon()
return icon;
string getTheme()
{
return theme;
theme= themeValue;
return;
return firstNumber+secondNumber;
}
return firstNumber-secondNumber;
return firstNumber*secondNumber;
return firstNumber/secondNumber;
};
int main() {
calculator.setTheme("Dark");
cout << "Theme (after theme change): " << calculator.getTheme() << endl;
Pointer to Class
Encapsulation - Quiz 3
Questions: 3
Start Quiz
object.property
This is known as the dot member selection operator (or simply Dot
Operator). It is used to access a class member through the object.
Example
class Phone {
public:
string brand;
string model;
int ram;
int storage;
Phone (string brandValue, string modelValue, int ramValue, int storageValue) {
brand = brandValue;
model = modelValue;
ram = ramValue;
storage = storageValue;
};
int main () {
Encapsulation - Quiz 4
Questions: 2
Start Quiz
Just like variables, objects are also stored in memory and take up
some space. Since they are stored in memory, they will also have an
address at which they are stored.
Example
Phone phone_var("Apple", "iPhone 11", 4, 64);
Here ptr is a pointer variable of type Phone* and points to the address
of phone_var.
ClassName object;
Example
Phone phone_var("Apple", "iPhone 11", 4, 64);
Note that arrow operators can be used on a class pointer but not on
pointers of primitive data types (int, float, etc).
int var = 5;
Let's write some code. Modify the main method to create a pointer
variable to object phone and print the ram using the arrow operator.
Do not use the dot operator.
Expected Output
#include <bits/stdc++.h>
class Phone {
public:
string brand;
string model;
int ram;
int storage;
brand = brandValue;
model = modelValue;
ram = ramValue;
storage = storageValue;
}
};
int main() {
//Print the ram using the arrow operator. Do not use the dot operator.
return 0;
Encapsulation
this pointer
Encapsulation - Quiz 5
Questions: 3
Start Quiz
Examples:
brand = brandValue;
model = modelValue;
ram = ramValue;
storage = storageValue;
brand = brandVal;
model = model;
ram = ram;
storage = storage;
OR
brand = brand;
}
Encapsulation - Quiz 6
Questions: 2
Start Quiz
this->brand = brand;
this->model = model;
this->ram = ram;
this->storage = storage;
}
void setBrand(string brand) {
this->brand = brand;
Here, "this" pointer refers to a pointer to the actual object on which the
function is being called. In the case of a constructor, it is the pointer to
the object that will be created by the constructor.
this->brand = brand;
Examples:
this->brand = brand;
return this->brand;
}
void incrementValue() {
this->setValue(this->value + 1);
Note that "this->" is mandatory only when we've another variable with
the same name as a class member, i.e., when there is ambiguity
between a variable name and a class member. This is why we didn't
have to use this-> when we were using a different parameter name.
#include <bits/stdc++.h>
class CalculatorApp{
private:
string name;
string icon;
string theme;
public:
this->name = name;
this->icon = icon;
this->theme = theme;
string getName()
return name;
}
string getIcon()
return icon;
string getTheme()
return theme;
}
void setTheme(string themeValue)
theme= themeValue;
return;
return firstNumber+secondNumber;
return firstNumber-secondNumber;
return firstNumber*secondNumber;
return firstNumber/secondNumber;
}
};
int main() {
calculator.setTheme("Dark");
cout << "Theme (after theme change): " << calculator.getTheme() << endl;
return 0;
}
Encapsulation
Encapsulation - Quiz 7
Questions: 4
Start Quiz
class CalculatorApp {
private:
string theme;
public:
this->theme = theme;
string getTheme() {
return this->theme;
}
void setTheme(string theme) {
this->theme = theme;
};
These methods are commonly known as getters and setters. They are
widely used in commercial applications where object-oriented
programming is used.