Btech C++unit 1
Btech C++unit 1
Btech C++unit 1
Function definitions
Macros
It offers the above features by importing them into the program with
the help of a preprocessor directive “#include”. These preprocessor
directives are used for instructing compiler that these files need to be
processed before compilation. There are of 2 types of header file:
Pre-existing header files: Files which are already available in C/C++
compiler we just need to import them.
User-defined header files: These files are defined by the user and can
be imported using “#include”.
Syntax:
#include <filename.h>
or
#include "filename.h"
1. #include<stdio.h>
7. #include<time.h>(Time header)
1. Operating Systems
2. Games
4.Banking Applications
Data hiding
Data hiding means hiding the internal data within the class to prevent
its direct access from outside the class.
C++ Polymorphism
Dynamic binding
Data members are the data variables and member functions are the
functions used to manipulate these variables and together these data
members and member functions defines the properties and behavior of
the objects in a Class.
In the example of class Car, the data member will be speed limit,
mileage etc and member functions can be apply brakes, increase speed
etc.
#include <iostream>
int main() {
return 0;
Example explained
Line 1: #include <iostream> is a header file library that lets us work with
input and output objects, such as cout (used in line 5). Header files add
functionality to C++ programs.
Line 2: Another thing that always appear in a C++ program, is int main().
This is called a function. Any code inside its curly brackets {} will be
executed.
Line 3: cout (pronounced "C -out") is an object used together with the
insertion operator (<<) to output/print text. In our example it will
output "Hello World".
// An abstract class
class Test
{
// Data members of class
public:
/* Other members */
};
A complete example:
#include<iostream>
class Base
int x;
public:
};
int y;
public:
};
int main(void)
Derived d;
d.fun();
return 0;
Output:
fun() called
Access specifier
Public
Private
Protected
Note: If we do not specify any access modifiers for the members inside
the class, then by default the access modifier for the members will be
Private.
1. Public: All the class members declared under the public specifier will
be available to everyone. The data members and member functions
declared as public can be accessed by other classes and functions too.
The public members of a class can be accessed from anywhere in the
program using the direct member access operator (.) with the object of
that class.
Example:
// access modifier
#include<iostream>
// class definition
class Circle
public:
double radius;
double compute_area()
return 3.14*radius*radius;
};
// main function
int main()
Circle obj;
obj.radius = 5.5;
return 0;
Output:
Example:
// access modifier
#include<iostream>
class Circle
private:
double radius;
public:
double compute_area()
{ // member function can access private
return 3.14*radius*radius;
};
// main function
int main()
Circle obj;
obj.radius = 1.5;
return 0;
Note: This access through inheritance can alter the access modifier of
the elements of base class in derived class depending on the mode of
Inheritance.
Example:
#include <bits/stdc++.h>
// base class
class Parent
protected:
int id_protected;
};
// sub class or derived class from public base class
public:
id_protected = id;
void displayId()
};
// main function
int main() {
Child obj1;
obj1.setId(81);
obj1.displayId();
return 0;
Output:
id_protected is: 81
The public data members are also accessed in the same way given
however the private data members are not allowed to be accessed
directly by the object. Accessing a data member depends solely on the
access control of that data member.
This access control is given by Access modifiers in C++. There are three
access modifiers : public, private and protected.
#include <iostream>
class Geeks
{
// Access specifier
public:
// Data Members
string geekname;
// Member Functions()
void printname() {
};
int main() {
Geeks obj1;
obj1.geekname = "Abhi";
obj1.printname();
return 0;
Output:
Geekname is: Abhi
What is a structure?
Structures in C++
Unlike Arrays, Structures in C++ are user defined data types which are
used to store group of items of non-similar data types.
struct structureName{
member1;
member2;
member3;
memberN;
};
struct Point
int x, y;
struct Point
int x, y;
};
int main()
Example:
// Data Members
int roll;
int age;
int marks;
// Member Functions
void printDetails()
cout<<"Roll = "<<roll<<"\n";
cout<<"Age = "<<age<<"\n";
cout<<"Marks = "<<marks;
In the above structure, the data members are three integer variables to
store roll number, age and marks of any student and the member
function is printDetails() which is printing all of the above details of any
student.
struct Point
};
#include <iostream>
struct Point {
int y = 1;
};
int main()
cout << "x = " << p1.x << ", y = " << p1.y<<endl;
p1.y = 20;
cout << "x = " << p1.x << ", y = " << p1.y;
return 0;
x=0, y=1
x=0, y=20
struct Point {
int x, y;
};
int main()
struct Point p1 = { 0, 1 };
#include <iostream>
struct Point {
int x, y;
};
int main()
P1. x=10;
p1.y= 20;
cout << "x = " << p1.x << ", y = " << p1.y;
return 0;
Output
x = 10, y = 20
constant
As the name suggests the name constants are given to such variables or
values in C/C++ programming language which cannot be modified once
they are defined. They are fixed values in a program. There can be any
types of constants like integer, float, octal, hexadecimal, character
constants, etc.
Defining Constants:
In C/C++ program we can define constants in two ways as shown below:
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: