Skip to player
Skip to main content
Skip to footer
Search
Connect
Watch fullscreen
Like
Bookmark
Share
Add to Playlist
Report
Concrete Classes in C++
SoftPrayog
Follow
7/10/2025
Concrete classes have no pure virtual functions, all data members and methods are completely defined. Concrete classes can be instantiated. https://www.softprayog.in/programming/concrete-classes-in-cpp
Category
🤖
Tech
Transcript
Display full video transcript
00:00
Hello and welcome to this video. I am Karnesh Jhouri and this video is about
00:05
concrete classes in C++. There are concrete classes and there are abstract
00:13
classes. As the name suggests concrete and abstract classes are opposites of
00:19
each other. The key difference is that concrete classes can be instantiated
00:25
whereas abstract classes cannot be instantiated. We can create objects of
00:32
concrete classes but not of abstract classes. Concrete classes must not contain
00:39
any pure virtual function. Abstract classes must have at least one pure
00:46
virtual function. So abstract classes have to be subclassed and the pure virtual
00:53
function needs to be implemented in the derived class. Concrete classes can
01:00
behave like built-in types. The data members are part of the definition and
01:05
objects can be created and initialized like the built-in types. You can have
01:12
local variables on the stack for objects, pass objects by value to functions and
01:18
copy objects using the assignment operator. And if concrete classes implement
01:24
operator overloading functions, you can use operators like plus, minus, star, slash,
01:30
like the built-in types. In fact concrete classes do better than the built-in types.
01:37
For example, vector is better than array because its size is not fixed at compile
01:43
time and error handling is better. Similarly, C++ strings are preferable to
01:50
C style strings. We'll have two examples of concrete classes. First, an arithmetic type
01:56
complex and second, a container class vector. This is an implementation of complex
02:04
complex class. It is a concrete class. There are two data members which are two
02:10
doubles a and b and they represent the real and imaginary parts of the complex
02:16
number a plus bi where i is equal to square root minus 1. There are three
02:24
constructors. The first one takes two arguments, creates an object of class complex
02:30
and initializes data members a and b to the arguments h and k.
02:36
The second constructor takes one argument, creates an object of class complex, initializes
02:43
the real part a to h and the imaginary part b is initialized to 0. Then there is a default
02:50
constructor with no arguments. It initializes real and imaginary parts a and b to 0. Now we
02:58
have two data members a and b. There should be some way to read and write to these data
03:03
members from the users of the class complex. For this, we have getter and setter methods.
03:10
The getter methods read and report the values and the setter method sets values. The first
03:18
one real constant reads the value of a. It is a getter method and next real double h is a setter
03:27
method which sets the value of a. Similarly, we have imac, getter and setter methods and we
03:35
have four operator overload functions for operators plus, minus, star for multiplication and slash
03:44
for division. Using these, we can add, subtract, multiply and divide complex objects. If we look
03:52
at any of the four operator overload functions, they take in one argument. For example, complex operator
04:01
plus constant complex reference z constant. There is only one argument z and if we look at its usage,
04:11
in the main function, we see something like complex c1 initialized to x plus y. So, how does x plus y happen
04:21
when the plus overload function takes in only one argument? The answer to this lies in the fact that
04:27
operator overload functions are non-static member functions of class complex. They are called for
04:34
objects of class complex. When a non-static member function is executed, an implicit pointer named this
04:42
is available which points to the object for which the member function is being executed. So, the first
04:50
parameter is the object for which member function is being executed. When you see the statement c1
04:57
initialized to x plus y, the expression x plus y needs to be evaluated. The compiler interprets x plus y as
05:06
x dot operator plus parenthesis y which means operator overload plus for object x and add object y to it.
05:16
So, y is added to x and the result is used for initializing c1. Let us quickly look at the main function.
05:26
x and y are two complex objects. x initialized to 2.0 real, 3.0 imaginary and y initialized to 3.3 real and 4.5 imaginary.
05:40
Then we have c1, c2, c3 and c4 complex objects initialized to x plus y, x minus y, x star y and x slash y respectively.
05:54
And we print c1, c2, c3 and c4. Next, we change the values for x and y and we recalculate and print.
06:04
We can compile and run this program and it prints results which are okay.
06:09
Vectors. Our second example is vectors. A vector is a container of other objects. A vector stores its
06:18
elements sequentially much like an array but it is not fixed size like array. Its size is dynamic.
06:25
It can grow or shrink at run time. In c++, vector is normally preferred over arrays and is in fact
06:35
most commonly used container in c++. And here is an implementation of vector class.
06:42
Looking at the program, first we have the data members.
06:45
The size of vector is sg and the type of sg is size t not int. The type size t is unsigned and is
06:56
guaranteed to be large enough to hold the size of the largest object on the system. Whereas int is
07:03
signed and there is no guarantee about its size. So size t is preferred type for variables that hold
07:11
size of something and it makes our program portable. The elements are stored in an array and double
07:17
pointer element points to an array of sg doubles. The constructor creates the vector from an initializer
07:25
list of double. It creates an array of doubles of the size of the list and sg is also initialized
07:32
from the size of the list. The elements are copied in the array. The destructor deletes the elements
07:38
and there is the important operator overload function for the square brackets. With this,
07:44
we can access the elements of a vector and there is the getter function which gives the size of the
07:49
vector. In the main function, we initialize vector v1 with initializer list and we print it in the for loop.
07:56
We have looked at concrete classes in this video. You can find all this information
08:03
at Soft Pryok website. Here is the QR code and the link is there in the description. Please
08:10
subscribe to Soft Pryok, click on the bell icon and enable notifications. Thanks very much for watching.
08:18
Take care and stay safe.
Recommended
30:31
|
Up next
Data Types and Declarations in C++
SoftPrayog
5/9/2025
0:29
Painted Storks
SoftPrayog
1/14/2025
9:41
How to Backup a WordPress Site using the Command Line
SoftPrayog
1/12/2025
10:47
How to take backups in Linux using the Command Line
SoftPrayog
1/11/2025
0:59
VIM editor - copy, paste and delete operations
SoftPrayog
1/10/2025
20:44
vi text editor in Linux
SoftPrayog
1/9/2025
32:53
Socket programming in C
SoftPrayog
1/8/2025
21:00
Signals in Linux
SoftPrayog
1/7/2025
15:22
sed command in Linux
SoftPrayog
1/6/2025
9:22
rsync file copy command
SoftPrayog
1/5/2025
17:32
What is an Operating System?
SoftPrayog
1/4/2025
15:40
ip command in Linux with examples
SoftPrayog
1/3/2025
7:48
htop command in Linux
SoftPrayog
1/2/2025
14:53
top command in Linux
SoftPrayog
12/31/2024
14:27
grep command in Linux
SoftPrayog
12/30/2024
30:34
Git Source Code Management Tutorial
SoftPrayog
12/29/2024
10:50
diff, patch and vimdiff commands in Linux
SoftPrayog
12/28/2024
31:06
Awk command in Linux
SoftPrayog
12/27/2024
35:38
Time in Linux
SoftPrayog
12/22/2024
13:52
Pthreads in C under Linux
SoftPrayog
12/21/2024
7:58
ps command in Linux
SoftPrayog
12/20/2024
12:54
Dining philosophers problem with solution
SoftPrayog
12/19/2024
10:00
Network Traffic Control with tc command in Linux
SoftPrayog
12/18/2024
7:54
Unix domain sockets
SoftPrayog
12/17/2024
21:05
File I/O in Linux
SoftPrayog
12/16/2024