Thursday, August 26, 2021

constructors in c++ with examples


Constructors:

Special function

Without return type

Name of class and constructor are same

Default constructors:

This constructor automatically created when we create an object

No need to call

This constructor has no argument pass

#include<iostream>
using namespace std;
class employee
{
    string name;
    int employee_id;
    int selary;
    public:
    employee()
    {
        cout<<"enter name"<<endl;
        cin>>name;
        cout<<"enter employee id"<<endl;
        cin>>employee_id;
        cout<<"enter selary"<<endl;
        cin>>selary;
    }
    void display()
    {
        cout<<"-----------------------"<<endl;
        cout<<"name             "<<name<<endl;
        cout<<"employee  "<<employee_id<<endl;
        cout<<"selary         "<<selary<<endl;
        cout<<"-----------------------"<<endl;
    }
};
int main()
{
    employee s2;
    s2.display();
    return 0;
}

Parameterized constructor:

This type of constructor has argument pass in the constructor

We call it and add parameters

#include<iostream>
using namespace std;
class employee
{
    string s_name;
    int roll_no;
    int s_marks;
    public:
    employee(string name,int roll ,int marks)
    {
        s_name=name;
        roll_no=roll;
        s_marks=marks;
    }
    void display()
    {
        cout<<"------------------------------"<<endl;
        cout<<"name       "<<s_name<<endl;
        cout<<"roll no    "<<roll_no<<endl;
        cout<<"marks      "<<s_marks<<endl;
        cout<<"------------------------------"<<endl;
    }
};
int main()
{
    employee s("waqas",17,487);
    s.display();
;
    return 0;
}

Copy constructors:

it used to copy of one object to another

initialization of an object with another object called copy constructor

We also need to call it for use

Syntax:

Class name (const class name & object name)

#include<iostream>
using namespace std;
class student
{
    string s_name;
    int roll_no;
    int s_marks;
    public:
    student (string name, int roll, int marks)
    {
        s_name=name;
        roll_no=roll;
        s_marks=marks;
    }
    student(student &s)
    {
        s_name=s.s_name;
        roll_no=s.roll_no;
        s_marks=s.s_marks;
    }
    void display()
    {
        cout<<"-----------------------------------"<<endl;
        cout<<"name                         "<<s_name<<endl;
        cout<<"roll no                      "<<roll_no<<endl;
        cout<<"marks                        "<<s_marks<<endl;
        cout<<"-----------------------------------"<<endl;
    }
};
int main()
{
    student s1("waqas",17,487);
    student s3(s1);
    cout<<"parameterized constuctor"<<endl;
    s1.display();
    cout<<"copy constutor"<<endl;
    s3.display();
    return 0;
}

types of inheritance


Types of inheritance

1.       Single inheritance

2.       Multiple inheritance

3.       Heretical inheritance

4.       Multilevel inheritance

5.       Hybrid inheritance

Single inheritance:

it has single base class and single derived class it consists of at least 2 classes

example:

animal derived from leaving things

car derived from vehicle

program:

in this example a single base and child classes exist so it is single

 inheritence

#include<iostream>
using namespace std;
class animal
{
    public:
    int legs=4;
    int tail=1;
};
class dogpublic animal
{
    public:
    void disp()
    {
    cout<<"i provide security in day because i sleep in day"<<endl;
}
};
int main()
{
    dog d1;
    cout<<"legs are ="<<d1.legs<<endl;
    cout<<"tail ="<<d1.tail<<endl;
    d1.disp();
}

multiple inheritance:

in this type of inheritance, a single child class derived from two or more classes

example:

petrol derived from liquid and fuel

child class derived from mother and father

example program:

#include<iostream>

Using namespace std;

Int main ()

{

Class father

{

};

Class mother

{

};

Class child: public father, mother

{

};

error ambiguity or diamond problem

sample A: display ();

sample. b: display ();

hieratical inheritance:

in this type of inheritance multiple child classes inherits from a single parent class

example:

civil, mechanical and electrical derived from engineer class

multilevel inheritance:

in this type of inheritance, the derived class inherit from a class that inherited from another derived class

child father grandfather

hybrid inheritance:

hybrid inheritance is a method where one or more types of inheritance are combined together

object oriented programming in c++

 

Objects oriented programing

 

The basic methodology in which we design code using classes and objects

It is the process of through which software are develops

The goal of object oriented programming is to crate reliable, reusable and extendable system

In oop classes and objects are created to make programming more secure and protected

Advantages of using oop

        I.            It provides data encapsulation

     II.            It also provides abstraction                        

  III.            Reuseabily of code

   IV.            We not need to write code again and again

      V.            Make code more readable and effective

 Objects:

An object is a state well defined behavior and unique entity

As we create a member of class name age we can access them by creating an object an access them

Classes

ü User defined data types

ü Data structure

ü Basic unit of oop

ü Combination of functions

 There are two types of classes

*   Base class/parent class/super class

*   Child classes/derived class/subclass

Parent class

A class whose properties inherited by sub class called base class

Child class

A class that inherits the properties of another class

there are four major concepts in oop

chick link below to access these topic details

1. inheritance


2. Abstraction


3. Encapsulation


4. polymorphism


inheritence in c++

inheritance:

it is a mechanism in which one class acquire the properties of another class allow us to create new class derived class from existing class parent class derived class inherits the function from base class and have additional functions to.

we use access specifiers to provide data security

access specifier:

        i.            public : using this public keyword we allow anyone to access data anywhere he wants

      ii.            protected: only child classes access data or class it self

    iii.            private :no one can access data only class access the data

 

Public

Protected

Private

Within class

Yes

Yes

Yes

Child class

Yes

Yes

No

Main

Yes

No

No


code:

#include<iostream>
using namespace std;
class animal
{
    public:
    int legs=4;
    int tail=1;
};
class dogpublic animal
{
    public:
    void disp()
    {
    cout<<"i provide security in day because i sleep in day"<<endl;
}
};
int main()
{
    dog d1;
    cout<<"legs are ="<<d1.legs<<endl;
    cout<<"tail ="<<d1.tail<<endl;
    d1.disp();
}

Monday, August 23, 2021

c++ complete course programs+video lectures


link of the link to access all c++ theory and lectures

 Introduction (hello word , addition programs)

link:

https://www.blogger.com/blog/post/edit/361065262909762075/5744036816798745000

Conditional statements(if ,if else even odd program, nested if else)

link:    

https://www.blogger.com/blog/post/edit/361065262909762075/4321632763089659504

While loop (1 to 100 counting)

link:

https://www.blogger.com/blog/post/edit/361065262909762075/8747005176889350477

Do while loop (print table of screen)

link:

https://www.blogger.com/blog/post/edit/361065262909762075/5309132814788548533

For loop(star pattern single)

link:

https://www.blogger.com/blog/post/edit/361065262909762075/7023092947001531760

Nested loop(stars pattern )

link:

https://www.blogger.com/blog/post/edit/361065262909762075/4694654888901428579

1 dimensional array

2 dimensional array

3 dimensional array

(arrays in c++ link)

link:

https://www.blogger.com/blog/post/edit/361065262909762075/1938344246104666196

Pointers

link:

https://www.blogger.com/blog/post/edit/361065262909762075/3347844906367721324

File handling(read + write operation)

link:

https://www.blogger.com/blog/post/edit/361065262909762075/1336231095055182294


file handling in c++ read + write operations

file handling in c++

Files are used to store data permanently in a storage devices.
File handling is a mechanism to store output of program in a file.
Two methods of file handing
Read data from file
Write operation
To use these function we must include stream file in our program

write operation:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int i;
    fstream file;
    file.open("wakey.txt",ios::out);
    for(int i=1;i<=10;i++)
    {
        file<<20<<"*"<<i<<"="<<20*i<<endl;
    }
    return 0;
}

output:
text type file in created in your computer
20*1=20
20*2=40
20*3=60
20*4=80
20*5=100
20*6=120
20*7=140
20*8=160
20*9=180
20*10=200

write operation video:

read operation in file handling:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int i;
    fstream file;
    file.open("wakey.txt",ios::in);
    if(!file)
    {
        cout<<"file no exist";
    }
    while(1)
    {
        char ch;
        file>>ch;
        if(file.eof())
        break;
        cout<<ch;
    }
    return 0;
}

output:
this time it read data from file and show output
20*1=20
20*2=40
20*3=60
20*4=80
20*5=100
20*6=120
20*7=140
20*8=160
20*9=180
20*10=200

video lecture:



pointers in c++ programming language

 Pointer refers to variable that hold the address of another variable or memory location

Pointer hold the address of same type

Char can hold char int can store int address

& used to store address



code:

#include <iostream>
using namespace std;
int main(){
   int *p;
   int a;
   a=2000;

   p = &a;

   cout<<"Address of a: "<<&a<<endl;
   cout<<"Address of a: "<<p<<endl;
   cout<<"Address of p: "<<&p<<endl;
   cout<<"Value of a: "<<*p;
   return 0;
}

output:

Address of a: 0x78fe14
Address of a: 0x78fe14
Address of p: 0x78fe18
Value of a: 2000

explaination:

Adress of a and p will be the same because we refers the value of a to &p and &p is showing in third line it store nothing but address of a and value is stored to *p;

videos explanation:



Sunday, August 15, 2021

built in and user define functions with examples



functions

A group of statement that together perform a task

Every c++ program has at least one function which is main.

Function consist of name of function return type and parameters

Void main()

Built-in function also called the library functions 

We need not to declare and define these functions because they are already written in c++.

We need to include these functions in program to use

For example #include<iostream> #include<math>

c/c++ allow a programmer to define their own functions

A user defined function groups code to perform a specific task and group of code written by user(programmer)

We need to call the function to use in the program. 

program of user define function:

Example 1:

#include<iostream>
using namespace std;
int sum()
{
    int a,b;
    cout<<"enter value of a "<<endl;
    cin>>a;
    cout<<"enter value of b "<<endl;
    cin>>b;
    cout<<"a+b ="<<a+b<<endl;
}
int main()
{
    sum();
}


user define function : example 2

#include<iostream>
using namespace std;
int sum(int a,int b)
{
    cout<<"a+b ="<<a+b<<endl;
}
int main()
{
    sum(10,67);
}

array in c++ example program+video lectures,1d array,2d array,3d array

arrays in c++
Arrays are used to store multiple values in a single variable, instead of declaring separate variable
Syntax : string car[5];
The index always start from zero
So in example [0],[1][2][3][4] that are indexes of car[5]
There are three types of an 


array


 1 dimensional array

 
 2 dimensional array

 
 3 dimensional array

1d array 

A simple data structure that stores a collection of similar type data in a contiguous block of memory

  it store data in the form of list

program:

#include<iostream>
using namespace std;
int main()
{
    int s[5],i,sum=0;
    cout<<"enter values of an array"<<endl;
    for(int i=0;i<5;i++)
    {
        cin>>s[i];
    }
    cout<<"-------------------------------------"<<endl;
    cout<<"your arrays"<<endl;
    for(int j=0;j<5;j++)
    {
        cout<<s[j]<<"       "<<endl;
    }
    return 0;
}

it store multiple values in a single data structure this program take five integer values from user and display on screen.

video lecture

it store data index is always start with zero so it take 1 less value that is given because array start with zero as you see in figure a[6]= index no

2 dimensional array:



#include<iostream>
using namespace std;
int main()
{
    int s[2][3],i,j,k,l;
    cout<<"enter values of an array"<<endl;
    for(int i=0;i<2;i++)
    {
        for(j=0;j<3;j++)
        
        {
            cin>>s[i][j];
        }
    }
        for(k=0;k<2;k++)
        {
            for(l=0;l<3;l++)
            {
        cout<<s[k][l]<<"    ";
    }
        }
    return 0;
}

video lecture:





3 dimensional array


#include<iostream>
using namespace std;
int main()
{
    int s[2][3][4],i,j,k,l,m,n;
    cout<<"enter values of an array"<<endl;
    for(int i=0;i<2;i++)
    {
        for(j=0;j<3;j++)
        {
        for(k=0;k<4;k++)
        
        {
            cin>>s[i][j][k];
        }
    }
}
        for(l=0;l<2;l++)
        {
            for(m=0;m<3;m++)
            {
                for(n=0;n<4;n++)
                {
        cout<<s[l][m][n]<<"    ";
    }
        
        }
        }
    return 0;
}


video lecture:










How to apply for Canada visa?

  Applying for a Canada Visa: A Comprehensive Guide Explore the Beauty of Canada - Your Step-by-Step Visa Application Guide Introduction Can...