0 ratings0% found this document useful (0 votes) 33 views174 pagesC++ Programming
Very nice pdf for coding purpose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
++ Programming
C++ history
History of C+ language is interesting to know. Here we are
going to discuss brief history of C++ language.
C+ programming language was developed in 1980 by Bjarne
‘Stroustrup at bell laboratories of AT&T (American Telephone
& Telegraph), located in U.S.A.
Bjarne Stroustrup is known as the founder of C+ language.
It was develop for adding a feature of OOP (Object Oriented
Programming) in C without significantly changing the C
‘component.
C++ programming is “relative” (called a superset) of C, it
means any valid C program is also a valid C++ program.
Let's see the programming languages that were developed before C+ language.
Language Year
Algol 1960
BCPL 1967
8 1970
‘Traditional C 1972
K&RC 1978
C++ 1980
C++ Features
Developed By
International Group
Martin Richard
Ken Thompson
Dennis Ritchie
Kernighan & Dennis Ritchie
Bjame Stroustrup
Edit with WPS Office
© sexed with OK ScannerC++ is object oriented programming language. It provides a lot of features that are given below.
simple
Machine Independent or Portable
Mid-level programming language
‘Structured programming language
Rich Library
Memory Management
Fast Speed
Pointers
Recursion
Extensible
Object Oriented
Compiler based
\Y//) Edit with WPS Office
OF seamed ith OK Scanner1) Simple
C++ is a simple language in the sense that it provides structured approach (to break the
problem into parts), rich set of library functions, data types etc.
2) Machine Independent or Portable
Unlike assembly language, c programs can be executed in many machines with little bit or no
change. But it is not platform-independent.
3) Mid-level programming language
C++ is also used to do low level programming. It is used to develop system applications such as
kernel, driver etc. It also supports the feature of high level language. That is why itis known as
mid-level language.
4) Structured programming language
C++ is a structured programming language in the sense that we can break the program into
parts using functions. So, itis easy to understand and modify.
5) Rich Library
C++ provides a lot of inbuilt functions that makes the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In C++ language, we can free the
allocated memory at any time by calling the freeQ function.
7) Speed
The compilation and execution time of C+ language is fast,
8) Pointer
C++ provides the feature of pointers. We can directly interact with the memory by using the
pointers. We can use pointers for memory, structures, functions, array etc.
9) Recursion
In C++, we can call the function within the function. It provides code reusability for every
function
10) Extensible
C++ language is extensible because it can easily adopt new features.
11) Object Oriented
C++ is object oriented programming language. OOPs makes development and maintenance
easier where as in Procedure-oriented programming language it is not easy to manage if code
grows as project size grows.
12) Compiler based
Edit with WPS Office
© sexed with OK ScannerC++ is a compiler based programming language, it means without compilation no C+ program
can be executed. First we need to compile our program using compiler and then we can execute
ur program,
C++ Variable
A variable is a name of memory location. It is used to store data. Its value can be changed and it
can be reused many times.
Itis a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable
type variable lst;
The example of declaring variable is given below:
int x
float y;
char 2;
Here, x,y,z are variables and int, float, char are data types.
We can also provide values while declaring the variables as given below:
int x=5,b=10; //declaring 2 variable of integer type
float f-30.8;
char
Rules for defining variables
A variable can have alphabets, digits and underscore.
A variable name can start with alphabet and underscore only. It can't start with digit
No white space is allowed within variable name.
A variable name must not be any reserved word or keyword e.g. char, float ete.
C++ Data Types
A data type specifies the type of data that a variable can store such as integer, floating,
character ete.
\V/4, Edit with WPS Office
© sexed with OK ScannerData Types in C++
‘There are 4 types of data types in C+ language.
‘Types:
Basic Data Type
Derived Data Type
Enumeration Data Type
User Defined Data Type
Basic Data Types
Data Types
char, float, double, ete
array, pointer, etc
enum
structure
‘The basic data types are integer-based and floating-point based. C++ language supports both
signed and unsigned literals.
\V/4, Edit with WPS Office
© sexed with OK Scanner‘The memory size of basic data types may change according to 32 or 64 bit operating system,
Let's see the basic data types. It size is given according to 32 bit OS.
Data Types Memory Size Range
char 1 byte “12810 127
unsigned char 1 byte 0t0 127
int 2byte “32,768 to 32,767
unsigned int 2byte oto 32,767
float Abyte
double Bbyte
C++ Keywords
A keyword is @ reserved word. You cannot use it as a variable name, constant name etc. A list of
32 Keywords in C++ Language which are also available in C language are given below.
auto break case char const continue default. = do.
double else enum extern float for goto if
int long register return. short signed sizeof static
struct switch typedef union unsigned void volatile while
A\list of 30 Keywords in C-++ Language which are not available in C language are given below.
asm dynamic
cast namespace reinterpret_cast bool
Edit with WPS Office
© sexed with OK Scannerexplicit new static_cast false catch
‘operator template friend private class
this inline public throw const_cast
delete mutable protected true ty
typeid ‘typename using virtual wehar_t
C++ Operators
Operators are symbols that perform operations on variables and values. For example, + is an
operator used for addition, while - is an operator used for subtraction.
Operators in C++ can be classified into 6 types:
Arithmetic Operators
‘Assignment Operators
Relational Operators
Logical Operators
Bitwise Operators
Other Operators
1. C++ Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For
example,
a+b;
Here, the + operator is used to add two variables a and b. Similarly there are various other
arithmetic operators in C++.
Operator Operation
\V/4, Edit with WPS Office
© sexed with OK Scanner+ Addition
- Subtraction
* Multiplication
I Division
% Modulo Operation (Remainder after division)
Increment and Decrement Operators
C++ also provides increment and decrement operators: ++ and ~ respectively.
++ increases the value of the operand by 1
~ decreases it by 1
2. C++ Assignment Operators
In C++, assignment operators are used to assign values to variables. For example,
JL assign 5to a
Here, we have assigned a value of 5 to the variable a.
Operator Example Equivalent to
. arb; a=b;
+ a+b; azatb;
asa-b;
aza*b;
I al=b; aza/b;
\V/4, Edit with WPS Office
© sexed with OK Scannera%
3. C++ Relational Operators
A relational operator is used to check the relationship between two operands. For example,
11 checks if a is greater than b
a>b;
Here, > is a relational operator. It checks if a is greater than b or not.
If the relation is true, it returns 1 whereas if the relation is false, it returns 0.
Operator Meaning
Is Equal To
Is Not Equal To
> Greater Than
Sgives.us false
3= Sgiveus false
3 Binary Shift Right
6 Other C++ Operators
\V/4, Edit with WPS Office
© sexed with OK ScannerHere's a list of some other common operators available in C++. We will learn about them in later
tutorials,
Operator
sizeof
C++ Identifiers
Description
returns the size of data type
returns value based on the condition
represents memory address of the
operand
accesses members of struct variables
or class objects
used with pointers to access the class
or struct variables
prints the output value
gets the input value
Example
sizeof(int); // 4
string result = (5 > 0) ? "even" :
odd’; // "even"
# // address of num
s1.marks = 92;
ptromarks = 92;
cout > num;
C++ identifiers in a program are used to refer to the name of the variables, functions, arrays, or
other user-defined data types created by the programmer. They are the basic requirement of any
language. Every language has its own rules for naming the identifiers.
In short, we can say that the C++ identifiers represent the essential elements in a program
which are given below:
Constants
Variables
Functions
\V/4, Edit with WPS Office
© sexed with OK ScannerLabels
Defined data types
Some naming rules are common in both C and C++. They are as follows:
Only alphabetic characters, digits, and underscores are allowed.
‘The identifier name cannot start with a digit, ie., the first letter should be alphabetical. After the
first letter, we can use letters, digits, or underscores.
In C++, uppercase and lowercase letters are distinct. Therefore, we can say that C++ identifiers
are case-sensitive.
A declared keyword cannot be used as a variable name.
C++ Program
Before starting the abed of C++ language, you need to learn how to write, compile and run the
first C+ program.
To write the first C++ program, open the C+ console and write the following code:
include
#include
void main()
4
clrser();
cout includes the standard input output library functions. It
provides cin and cout methods for reading from input and writing to output respectively.
‘include includes the console input output library functions. The getch() function is
defined in conio.h file.
void main() The main() function is the entry point of every program in C++ language. The void
keyword specifies that it retums no value.
cout
using namespace std;
int maing)
{
cout
using namespace std;
int maing)
{
int firstNumber, secondNumber, sumOfTwoNumbers;
cout > firstNumber >> secondNumber,
// sum of two numbers in stored in variable sumOfTwoNumbers
sumOfTwoNumbers = firstNumber + secondNumber,
1/ Prints sum
cout
using namespace std;
int main
{
cout
using namespace std;
int main)
{
int divisor, dividend, quotient, remainder,
cout > dividends;
cout > divisor,
quotient = dividend / divisor,
remainder = dividend %
or;
cout
using namespace std;
int maing)
{
inta
b= 10, temp;
cout
using namespace std;
int main,
{
char ¢;
cout > ¢;
cout
using namespace std;
int main {
int number,
\V/4, Edit with WPS Office
© sexed with OK Scannercout > number;
1/ checks if the number is positive
if (number > 0) {
cout
using namespace std;
int main {
int number;
cout > number,
if (number >= 0) {
cout
using namespace std;
int main
int num=66;
if(num
using namespace std;
int main 0 {
int num;
coutesnum;
if (num % 2 == 0)
{
cout
using namespace std;
int main () {
int num;
coute>num;
if (rum 100)
{
cout= 0 && num = 50 && num = 60 && num = 70 && num = 80 && num = 90 && num
using namespace std;
int maing)
{
float n1, n2,n3;
cout > n1>>.n2>>n3;
f(a >= n2.8& m1
{
cout = n1 && n2>=n3)
{
cout = nt && n3 >= m2) {
cout
using namespace std;
int main,
{
char ¢;
int isLowercaseVowel, isUppercase Vowel,
cout >c;
// evaluates to 1 (true) ifc is a lowercase vowel
isLowercaseVowel = (c =='a' Ile
// evaluates to 1 (true) if ¢ is an uppercase vowel
Elle
isUppercaseVowel = (c =="A' llc
// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
‘Olle
if (isLowercaseVowel || is UppercaseVowel)
cout oe
ms
using namespace std; eas
iment a
int num; .
coutnum;
switch (num)
{
case 10: coute
using namespace std;
int main {
char oper;
float num, num2;
cout > oper,
cout > num’ >> num2;
‘switch (oper) {
case '#:
cout
using namespace std;
int main ()
// local variable declaration:
char grade = 'D;
\V/4, Edit with WPS Office
© sexed with OK Scannerswitch(grade) {
case'A'
cout
using namespace std;
int main { !
O44)
coute
using namespace std;
int maid {
int num, sum;
sum
cout > num;
for (int count = 1; count
using namespace std;
int main)
{
intn;
unsigned long long factorial
cout > n;
if(n
using namespace std;
int main {
int n, 1 = 0,12 = 1, nextTerm = 0;
cout >;
cout
using namespace std;
int maind
«
int rows
cout > rows;
fori rows; +i)
\V/4, Edit with WPS Office
© sexed with OK Scannerfor(int j= 1; )
using namespace std;
int main)
{
int rows;
cout > rows;
for(int i= 1;i
using namespace std;
int main,
{
int rows;
cout > rows;
for(int i= rows; i>=
{
for(int j= 1;
1)
+4)
cout
using namespace std;
int maing,
{
int rows;
cout > rows;
for(int = rows; i>=1;~i)
{
for(int j= 15)
using namespace std;
int maing)
{
int rows, number = 1;
cout > rows;
for(i
{
for(int} = 1;
using namespace std;
\V/4, Edit with WPS Office
© sexed with OK Scannerint main)
{
int rows, i,j, space;
cout > rows;
for(i = 1;
using namespace std;
int maid {
inti= 1;
// ihile loop from 1 to 5
while ((
using namespace std;
int main {
int number;
\V/4, Edit with WPS Office
© sexed with OK Scannerint sum = 0;
// take input from the user
cout > number,
while (number >= 0) {
// add all positive numbers
sum += number;
// take input again if the number is positive
cout > number,
}
// display the sum
cout
using namespace std;
int main0{
int
\V/4, Edit with WPS Office
© sexed with OK Scanner/* The loop would continue to print
* the value of i until the given con
+ ic=6 retums false,
4
while(ic=6){
cout
using namespace std;
int main {
inti= 1;
1/ d0...while loop from 1 to
do{
cout
using namespace std;
int main( {
int number = 0;
int sum = 0;
do{
sum += number;
\V/4, Edit with WPS Office
© sexed with OK Scanner//take input from the user
cout > number;
)
while (number >= 0); i fom |
1/ display the sum
cout
using namespace std;
int maind {
for (int i=
si
using namespace std;
int maint
int num =10;
while(num
using namespace std;
int maint
int var,
for (var =200; var>=10; var ~) {
cout
using namespace std;
int maint
int num=2;
switch (num) {
case 1: cout
using namespace std;
int main)
continue;
}
coutecic
using namespace std;
int main0{
int
while (/>=0) {
continue;
}
coute
using namespace std;
int mainO{
\V/4, Edit with WPS Office
© sexed with OK Scannercontinue;
+
coute
using namespace std;
int main,
{
int origNum, num, rem, sum =
cout > origNum;
\V/4, Edit with WPS Office
© sexed with OK Scannernum = origNum;
while(num != 0)
{
digit = num % 10;
num /= 10;
}
if(sum
rigNum)
cout
using namespace std;
int main0{
int num; cout>num;
if (num % 2==0)
goto print;
y
else {
cout
using namespace std;
int main)
\V/4, Edit with WPS Office
© sexed with OK Scannerfloat num, average, sum = 0.0;
inti,n;
cout >n;
for(i = T;i> num;
if(num
using namespace std;
int maing)
{
ineligible:
coutage;
if (age 0 1 2 3
Advantages of C++ Array
Code Optimization (less code)
Random Access
Easy to traverse data
Easy to manipulate data
Easy to sort data ete.
Disadvantages of C++ Array
Fixed size
C++ Array Types:
‘There are 2 types of arrays in C++ programming
Single Dimensional Array
Multidimensional Array
G++ Array Declaration
\V/4, Edit with WPS Office
© sexed with OK Scannerdatatype arrayNamelarraySizel
For example,
int x{6];
Here,
int -type of element to be stored
x- name of the array
6- size of the array
‘Access Elements in C++ Array
In C++, each element in an array is associated with a number. The number is known as an array
index. We can access elements of an array by using those indices.
1/ syntax to access array elements
arraylindex};
Consider the array x we have seen above.
ArrayMembers—> x(0] xi], xia] xls] xl4] x8)
— o 1 2 3 4 5
Elements of an array in C++
Array Indices
Few Things to Remember:
The array indices start with 0. Meaning x0] is the first element stored at index 0.
If the size of an array is n, the last element is stored at index (n-1). In this example, x[5] is the
last element.
Elements of an array have consecutive addresses. For example, suppose the starting address:
of x{0] is 21204. Then, the address of the next element x{1] will be 2124d, the address of x[2] will
be 2128d and so on.
Here, the size of each element is increased by 4. This is because the size of int is 4 bytes.
(C+ Array Initialization
\V/4, Edit with WPS Office
© sexed with OK ScannerIn C++, it's possible to initialize an array during declaration. For example,
1/ declare an
int x[6] = {19, 10, 8, 17, 9, 15};
lize and array
ArrayMembers —> x(0] >it} xi2]— xf] xl] x8]
— o 1 2 3 4 5
C++ Array elements and their data
Array Indices
Another method to initialize array during declaration:
1/ declare and initialize an array
int x{] = {19, 10, 8, 17, 9, 15};
Here, we have not mentioned the size of the array. In such cases, the compiler automatically
computes the size.
Example: Calculate Average of Numbers Using Arrays
include
using namespace std;
int main
{
inti:
float num[100], sum=0.0, average;
cout > n;
while (n > 100 | n > n;
}
for(i = 0; i > numiil;
sum += numlil;
y
average = sum / n;
cout
using namespace std;
int maing)
{
inti n;
float arr{1 00];
\V/4, Edit with WPS Office
© sexed with OK Scannercout >
cout > arrlil;
}
// Loop to store largest number to arr{0]
for(i = 1yi if you want to find the smallest element
if(arrf0]
using namespace std;
int main)
{
int arr{5]=(10, 0, 20, 0, 30}; //creating and initializing array
Iikraversing array
for (int i= 0;i
using namespace std;
int main {
int test{3][2] = 42, -5},
{4,0},
9.
J/.use of nested for loop
// access rows of the array
for (int i= 0; 1
using namespace std;
int main {
int numbers{2][3};
cout > numbers[ilj;
Edit with WPS Office
© sexed with OK Scannercout
using namespace std;
int main
{
int r,c, a{100][100), b[1 00][100], sum[100]{109}, i,j;
cout > r;
cout c
cout > ali
// Storing elements of second matrix entered by user.
cout > blll
1/ Adding Two matrices
for(i = 0;1
using namespace std;
int mainO {
int a[10]{10], transpose[10][1 0], row, column, i,j
cout > row >> column;
cout > alll;
\V/4, Edit with WPS Office
© sexed with OK Scanner// Printing the @ matrix
cout
using namespace std;
int main,
{
char str{100);
cout > str;
\V/4, Edit with WPS Office
© sexed with OK Scannercout > str;
cout > works as scanf() in C and considers a space *" has a
terminating character.
Example 2: C++ String to read a line of text
C+ program to read and display an entire line entered by user.
#include
using namespace std;
int main)
{
char str[100];
cout
using namespace std;
int main)
{
string str = "C++ Programming is awesome";
char checkCharacter
int count
for (int i = 0; i
using namespace std;
int main {
string line;
string temp =";
cout = ‘a’ && linefi] = 'A’ && line[i]
\V/4, Edit with WPS Office
© sexed with OK Scannerusing namespace std;
int main {
string st
‘C+ Programming’;
// you can also use str.ength()
cout
using namespace std;
int main
{
string s1, s2, result;
cout
using namespace std;
int main)
{
char line[150];
int vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces =
cout ="0' 88 line
{
+#digits;
}
else if (lineli}
{
+#spaces;
&& lineli)
‘VII Cinefi
cout
void greet() {
using namespace std;
1/ declaring a function
void greet() {
cout
using namespace std;
// display a number
void displayNum(int n1, float n2) {
cout
void displayNum(int n1, double n2) {
using namespace std;
1/ declaring a function
int ada(int a, int b) {
return (a +b);
int main0 {
\V/4, Edit with WPS Office
© sexed with OK Scannerint sum;
// calling the function and storing
// the returned value in sum
sum = add(100, 78);
cout
int add(int a, int b) ¢
return (a + b);
-—?
int main() { funetion
int sum; call
sum = add(100, 78);
>
Working of C++
Funetion with retum statement
Notice that sum is a variable of int type. This is because the return value of add() is of int type.
Function Prototype
\V/4, Edit with WPS Office
© sexed with OK ScannerIn C++, the code of function declaration should be before the function call. However, if we want
to define a function after the function call, we need to use the function prototype. For example,
function prototype
void addtint, int);
int main {
1/ calling the function before declaration
add(5, 3);
return 0;
11 function definition
void add(int a, int b) {
cout
using namespace std;
// function prototype
\V/4, Edit with WPS Office
© sexed with OK Scannerint add{ int, int);
int main {
int sum;
// calling the function and storing
// the returned value in sum
sum = add(100, 78);
cout
#include
Using namespace std;
int main {
double number, squareRoot;
number = 25.0;
// sqrt() is a library function to calculate the square root
squareRoot = sqrt(number);
cout to use the sqrt) function.
‘Types of User-defined Functions in C++
That's why we need to use
For better understanding of arguments and retum in functions, user-defined functions can be
\V/4, Edit with WPS Office
© sexed with OK Scannercategorised as:
Function with no argument and no return value
Function with no argument but return value
Function with argument but no return value
Function with argument and return value
Consider a situation in which you have to check prime number. This problem is solved below by
making user-defined function in 4 different ways as mentioned above.
Example 1: No arguments passed and no return value
# include
using namespace std;
void prime();
int mainO)
{
// No argument is passed to prime()
prime();
return 0;
// Return type of function is void because value is not returned
void prime()
{
int num, j, flag = 0;
cout > num;
\V/4, Edit with WPS Office
© sexed with OK Scanner2;i
using namespace std;
int prime(j
\V/4, Edit with WPS Office
© sexed with OK Scannerint main
{
int num, i flag = 0;
// No argument is passed to prime()
num = prime();
if (flag == 1)
{
cout> n;
return n;
}
In the above program, prime() function is called from the main() with no arguments.
prime( takes a positive integer from the user. Since, return type of the function is an int, it
returns the inputted number from the user back to the calling main() function.
Then, whether the number is prime or not is checked in the main() itself and printed onto the
screen,
Example 3: Arguments passed but no return value
include
using namespace std;
void prime(int
int main,
{
int num;
cout > num;
// Argument num is passed to the function prime()
prime(num);
return 0;
11 There is no return value to calling function, Hence, return type of function is void. */
void prime(int n)
\V/4, Edit with WPS Office
© sexed with OK Scannerinti, flag = 0;
for (i= 2:1
using namespace std;
int prime(int n);
\V/4, Edit with WPS Office
© sexed with OK Scannerint main)
{
int num, flag = 0;
cout > num;
// Argument num is passed to check() function
flag = prime(num);
if(flag == 1)
cout
using namespace std;
class Student
{
private:
char name[30];
int rollno;
\V/4, Edit with WPS Office
© sexed with OK Scannerfloat per;
Public:
void getdata()
{
coute>name;
couterolino;
coute>per,
void putdata()
{
cout
using namespace std;
class Employee
{
private:
char Emp_Name[30];
int Emp_td;
float Salary,
public:
void getdata()
{
couts>Emp_Name;
cout>Emp_Id;
coutss'Enter Employee Salary:
cin>>Salary;
void putdata()
{
cout
using namespace std;
class Book
{
private:
char Book_Name(30];
char Author[30];,
float Price;
public:
void getdata()
{
cout> Book_Name;
coute>Author,
cout>Price;
\V/4, Edit with WPS Office
© sexed with OK Scannervoid putdata()
{
cout
using namespace std;
class Student
{
private:
char name[30};
int rolino;
float per;
public:
void getdata()
\V/4, Edit with WPS Office
© sexed with OK Scannercout>name;
couts>rolino;
couts>per;
)
void display)
{
coute
using namespace std;
class Book
{
private:
char Book Name{30};
char Author{30};
float Price;
int Page_No;
int year,
publi
void getdata()
{
coutss"Enter Book Data\n’;
coute> Book Name;
coute>Author;
coutss"Enter Book Price:
cin>>Price;
cout>Page_No;
cout>year;
void putdata()
{
cout
using namespace std;
class Employee
{
private:
char Emp_Name(30);
int Emp_ld;
float Salary;
public:
void getdata()
{
\V/4, Edit with WPS Office
© sexed with OK Scannercoute>Emp_Name;
couteEmp_Id;
coute>Salary;
void display()
{
cout
using namespace std;
class Book
{
private:
char Book Name[30];
char Author[30};
float Price;
publi
void getdata()
{
couts> Book_Name;
coute>Author,
cout>Price;
void display()
{
cout Constructor have the same name as class
> Constructor do not return any value
>» They cannot be inherited
> Cannot refers to address
> They can have default argument
‘Syntax
Edit with WPS Office
© sexed with OK Scannerclass user_name
{
Private:
protected:
public:
user_name(); //Constructor Declare
user_name::user_name() //Constructor defined
{
Example:-
class Rectangle
{
Edit with WPS Office
© sexed with OK Scannerint width height;
public:
Rectangle(); //constructor declared
int area(void);
‘
Rectangle:Rectangle()
{
Types of Constructor
1) Default Constructor
2) Parameterized Constructor
3) Copy Constructor
4) Dynamic Constructor
1) Default Constructor: - A constructor which has no argument is known as.
default constructor. It is invoked at the time of creating object
‘Syntax:
className( );
Program for default constructor
#include
Edit with WPS Office
© sexed with OK Scannerusing namespace std;
1/ declare a class
class Wall (
private:
double length;
public:
// create a constructor
Wall {
//initialize private variables
length = 5.5;
cout
using namespace std;
class construct
{
public:
int a, b;
\V/4, Edit with WPS Office
© sexed with OK Scanner// Default Constructor
construct()
{
a=10;
b= 20;
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout
using namespace std;
class Add
public:
//Parameterized constructor
Ada(int num1, int num2) {
cout
using namespace std;
1/ declare a class
class Wall {
private:
double length;
double height;
public:
// create parameterized constructor
Wall(double len, double hgt) {
/Lipitialize private variables
length = len;
height = hgt;
}
double calculateArea() {
\V/4, Edit with WPS Office
© sexed with OK Scannerreturn length * height;
b
int main( {
1/ create object and initialize data members
Wall wallt(10.5, 8.6);
Wall wall2(8.5, 6.3);
cout
using namespace std;
\V/4, Edit with WPS Office
© sexed with OK Scannerclass copyconstructor
{
private:
int x,y; //data members
public:
copyconstructor(int x1, int y1)
{
x=xt;
yey;
}
/* Copy constructor */
copyconstructor (const copyconstruetor &sam)
{
x= samx;
y=samy;
void display()
{
coutcexc
Using namespace std;
class Date
{
public:
int day, month, year,
Date()
{
cout
#includesstring.h>
using namespace std;
class Sample
{
char *name;
int length;
\V/4, Edit with WPS Office
© sexed with OK Scannerpublic:
‘Sample()
{
length = 0;
name = new char{ length + 1];
Sample (char *s)
{
length = strlen(s);
name = new char[ length + 1];
strepy(name , s );
}
void display()
(
cout
#includescstring>
using namespace std;
class String
{
char *name;
int length;
public :
String Q)
{
length=0;
name=new char[length+1];
}
‘String (char *s) // constructor 2
{
length=strlen(s);
name=new charflength+1]; //one additional
strepy(name.s); // character for \O
\V/4, Edit with WPS Office
© sexed with OK Scanner}
Void display()
{
std:cout
using namespace std;
class Rect{
private:
int area;
public:
Rect(){
area = 0;
}
Reet(int a, int b){
area=a*b;
}
void display(){
cout
using namespace std;
class simple_interest
float principle, time, rate interest;
public:
simple_interest (float a, float b, float c) {
principle = a;
}
void display () {
interest =(principle* rate* time)/100;
cout
using namespace std;
//structure definition with private and public members
class Student (
private
int No;
float perc;
public:
[constructor
Student(int r, float p)
//function to read details
void read(void)
{
cout > rNo;
cout > perc;
}
//funetion to print details
void print(void)
{
\V/4, Edit with WPS Office
© sexed with OK Scannercout > roll_number,
cout > percentage;
//declaring and initialize the object
struct Student sta(roll_number, percentage);
//print the value
cout The destructor must have the same name as the class name with a
tilde(~) prefix.
> It never takes any arguments and no return value.
>» It cannot be declared as static volatile and constant
> It takes no argument and therefore cannot be overloaded
» It should have public access in the class declaration.
‘Syntax:-
~className();
Program for Destructor
#include
using namespace std;
class Employee
{
public:
Employee()
{
Edit with WPS Office
© sexed with OK Scannercoute
using namespace std;
class HelloWorld{
public:
1/Constructor
HelloWorld(){
cout
using namespace std;
class A
{
int num,
static int count;
public:
//Constructor
A(int n=0);
//Destructor
~A0;
Static int getcount();
i
int A::count:
AzA(int n)
numer
count++;
cout
Using namespace std;
class Account {
public:
feat salary = 60000;
‘class Programmer: public Account {
public:
float bonus = 5000;
ys
int main(void) {
Programmer p1;
cout
using namespace std;
class A
{
inta
int
public:
int mul()
\V/4, Edit with WPS Office
© sexed with OK Scanner{
inte = atb;
return ¢;
%
class B: private A
{
public:
void display()
{
int result = mulQ;
std::cout
using namespace std;
1/ base class
class Vehicle
NY
public:
Vehicle()
{
cout
Using namespace std;
\V/4, Edit with WPS Office
© sexed with OK Scanner