0% found this document useful (0 votes)
18 views116 pages

unit 1 oodp basic cpp full-1

The document provides an overview of C++ programming concepts, including constants, variables, arrays, pointers, classes, and use case diagrams. It explains the rules for declaring constants and variables, the structure and usage of arrays, and the role of pointers in memory management. Additionally, it covers class definitions, constructors, destructors, and relationships among classes, along with guidelines for creating use case diagrams.

Uploaded by

simonoct8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views116 pages

unit 1 oodp basic cpp full-1

The document provides an overview of C++ programming concepts, including constants, variables, arrays, pointers, classes, and use case diagrams. It explains the rules for declaring constants and variables, the structure and usage of arrays, and the role of pointers in memory management. Additionally, it covers class definitions, constructors, destructors, and relationships among classes, along with guidelines for creating use case diagrams.

Uploaded by

simonoct8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 116

OODP-UNIT 1

Difference between Cand C++


Structure of C++
CONSTANTS
• Constants are identifiers whose value does not change. While variables
can change their value at any time, constants can never change their
value.
• Constants are used to define fixed values such as Pi or the charge on an
electron so that their value does not get changed in the program even by
mistake.
• A constant is an explicit data value specified by the programmer.
• The value of the constant is known to the compiler at the compile time.
Declaring Constants
• Rule 1 Constant names are usually written in capital letters to
visually distinguish them from other variable names which
are normally written in lower case characters.
• Rule 2 No blank spaces are permitted in between the #
symbol and define keyword.
• Rule 3 Blank space must be used between #define and
constant name and between constant name and constant
value.
• Rule 4 #define is a preprocessor compiler directive and not a
statement. Therefore, it does not end with a semi-colon.
Cascading of Input or Output Operators
• << operator –It can use multiple times in
the same line.
• Its called Cascading
• Cout ,Cin can cascaded

• For example
• cout<<“\n Enter the Marks”;
• cin>> ComputerNetworks>>OODP;
Formatted Input and Output Operations

Formatted I/O

I/O class
Manipulat
function and
ors
flages

#include<iostream.h>
#define PI 3.14159
main()
{
cout.precision(3);
cout.width(10);
cout.fill(‘*’);
cout<<PI;
Output
*****3.142
Formatting Output Using Manipulators
• C++ has a header file iomanip.h that
contains certain manipulators to format the
output
Data Types in C++
Variables

A variable is the content of a memory location that stores a certain value. A variable is identified or denoted by a variable name.
The variable name is a sequence of one or more letters, digits or underscore, for example: character_
Rules for defining variable name:
 A variable name can have one or more letters or digits or underscore for example character_.
 White space, punctuation symbols or other characters are not permitted to denote variable name.
 A variable name must begin with a letter.
 Variable names cannot be keywords or any reserved words of the C++ programming language.
 Data C++ is a case-sensitive language. Variable names written in capital letters differ from variable names with the same
name but written in small letters.

01 Local Variables 03 Instance variables

02 Static Variables 04 Final Variables


Variables

LocalLocal
variable:
Variables These Instance variable:
Instance Variables Static Static Variables
variables: Constant Variables
Constant is
are the variables These are the variables Static variables are
something that
which are declared which are declared in also called as class
doesn't change. In C
within the method of a a class but outside a variables. These
language and C++
class. method, constructor or variables have only
we use the keyword
any block. one copy that is shared
const to make
Example: Example: by all the different
program elements
public class Car { public class Car { objects in a class.
constant.
public: private: String Example:
Example:
void display(int m){ color; public class Car {
const int i = 10;
// Method // Created an instance public static int
void f(const int i)
int model=m; variable color tyres;
class Test
// Created a local Car(String c) // Created a class
{
variable model { variable void init(){
const int i;
cout<<model; color=c; tyres=4;
};
} }} }}
C++ Arrays
Arrays
Used to store a collection of elements (variables)

type array-name[size];

Meaning:
This declares a variable called <array-name> which contains <size>
elements of type <type>
The elements of an array can be accessed as: array-name[0],…array-
name[size-1]

Example:
int a[100]; //a is a list of 100 integers, a[0], a[1], …a[99]
double b[50];
char c[10];
Memory representation
Array example
//Read 100 numbers from the user
#include <iostream.h>

void main() {

int i, a[100], n;

i=0; n=100;
while (i<n) {
cout << “Input element “ << i << “: ”;
cin >> a[i];
i = i+1;
}
//do somehing with it ..
}
Example
Two dimensional array
Example
Problems
Write a C++ program to read a sequence of (non-negative) integers from the user
ending with a negative integer and write out

• the average of the numbers


• the smallest number
• the largest number
• the range of the numbers (largest - smallest)

• Example:
– The user enters: 3, 1, 55, 89, 23, 45, -1
– Your program should compute the average of {3, 1, 55, 89, 23, 45} etc
Pointers
• Pointer is variable in C++
• It holds the address of another variable
• Syntax data_type *pointer_variable;
• Example int *p,sum;
Assignment
• To assign the address of variable to pointer-ampersand symbol (&)
• p=&sum;
• integer type pointer can hold the address of another int variable
• P=&sum;//assign address of another variable
• cout<<&sum; //to print the address of variable
• cout<<p;//print the value of variable
How to use it
• Example of pointer
#include<iostream.h>
using namespace std;
int main()
{ int *p,sum=10; Output:
p=&sum; Address of sum : 0X77712
cout<<“Address of sum:”<<&sum<<endl;Address of sum: 0x77712
cout<<“Address of sum:”<<p<<endl; Address of p: 0x77717
cou<<“Address of p:”<<&p<<endl; Value of sum: 10
cout<<“Value of sum”<<*p;
}
Pointers and Arrays
• assigning the address of array to pointer don’t
use ampersand sign(&)
#include <iostream>
using namespace std; OUTPUT:
int main(){ 0
//Pointer declaration 1
int *p; 2
//Array declaration 3
int arr[]={1, 2, 3, 4, 5, 6}; 4
//Assignment 5
p = arr; 6
for(int i=0; i<6;i++){
cout<<*p<<endl;
//++ moves the pointer to next int position
p++;
}
return 0;
This Pointers
• this pointer hold the adderss of current object
• int num;
• This->num=num;
Function using pointers
void swap(int *a,int *b)
{
#include<iostream>
int c;
using namespace std;
void swap(int *a ,int *b ); c=*a;
//Call By Reference *a=*b;
int main() *b=c;
{ }
int p,q;
cout<<"\nEnter Two Number You Want To Swap \n";
cin>>p>>q; Output:
swap(&p,&q); Enter Two Number You Want to
cout<<"\nAfter Swapping Numbers Are Given below\n\n"; Swap
cout<<p<<" "<<q<<" \n"; 10 20
return 0;
After Swapping Numbers Are
}
Given below
20 10
Type conversion and type
casting
• Type conversion or typecasting of variables refers to
changing a variable of one data type into another. While type
conversion is done implicitly, casting has to be done
explicitly by the programmer.
Type Casting
• Type casting an arithmetic expression tells the compiler to represent the value
of the expression in a certain format.
• It is done when the value of a higher data type has to be converted into the
value of a lower data type.
• However, this cast is under the programmer’s control and not under the
compiler’s control. The general syntax for type casting is
destination_variable_name=destination_data_ty--pe(source_variable_name);

float sal=10000.00;
int income;
Income=int(sal);
Class and objects
Class:

• Class is a user defined data type,


• It holds its own data members and member functions,
• It can be accessed and used by creating instance of that class.
• The variables inside class definition are called as data members and
the functions are called member functions
Class:

• Class is a user defined data type,


• It holds its own data members and member functions,
• It can be accessed and used by creating instance of that class.
• The variables inside class definition are called as data members and
the functions are called member functions
Define a Class Type
Syntax: Example:
Name of
the class class Rectangle
keyw class Class_name {
ord { private:
permission_label:
int width;
member;
int length;
Body permission_label:
member; public:
... void set(int w, int
}; l);
int area();
};

60
Object creation
Example for objects
- Data members Can be of any type,
built-in or user-defined.
This may be,
• non-static data member
Each class object has its own copy

• static data member


Acts as a global variable
68
• Static data member is declared using the static keyword.
• There is only one copy of the static data member in the class. All the
objects share the static data member.
• The static data member is always initialized to zero when the first class
object is created.

Syntax:
static data_type datamember_name;
Here
static is the keyword.
data_type – int , float etc…
datamember_name – user defined

69
Static Data Member
Rectangle
class Rectangle r1;
{ Rectangle
r2;
private: Rectangle
int width; r3;

int length; count

static int count;


r1 r2
public: width width
void set(int w, int length length
l); width
r3 length
int area();
}
- Data members Can be of any type,
built-in or user-defined.
This may be,
• non-static data member
Each class object has its own copy

• static data member


Acts as a global variable
71
• Static data member is declared using the static keyword.
• There is only one copy of the static data member in the class. All the
objects share the static data member.
• The static data member is always initialized to zero when the first class
object is created.

Syntax:
static data_type datamember_name;
Here
static is the keyword.
data_type – int , float etc…
datamember_name – user defined

72
Static Data Member
Rectangle
class Rectangle r1;
{ Rectangle
r2;
private: Rectangle
int width; r3;

int length; count

static int count;


r1 r2
public: width width
void set(int w, int length length
l); width
r3 length
int area();
}
Example program for static
#include <iostream>

using namespace std;


void Test(){

static int x = 1;
x = ++x;

int y = 1;
y = ++y;
cout<<"x = "<<x<<"n";
cout<<"y = "<<y<<"n";
}
int main()
{
Test();
Test();

return 0;
}
Static Member Functions in C++

• Properties of static member functions:


• A static function can only access other static
variables or functions present in the same
class
• Static member functions are called using the
class name. Syntax-
class_name::function_name( )
include <iostream>
#
Example for static member Int main()
{
using namespace std; Example example1, example2;
class Example{
example1.set_n();
static int Number;
example2.set_n();
int n;

public: example1.show_n();
example2.show_n();
void set_n(){
Example::show_Number();
n = ++Number;
} return 0;
}
void show_n(){

cout<<"value of n = "<<n<<endl;
}

static void show_Number(){

cout<<"value of Number = "<<Number<<endl;


}

};

int Example:: Number;


Use case diagram
Definition:
• A diagram that shows a set of use cases and
actors and their relationships. Use cases
represent system functionality, the
requirements of the system from the user’s
perspective.
Notations
• use case
• A description of a set of sequences of
actions, including variants, that system
performs that yields an observable value to
an actor.
Notations
• actor
• The people or systems that provide or
receive information from the system; they
are among the stakeholders of a system.
Notations
• include
• Specifies that the source use case explicitly
incorporates the behaviour of another use
case at a location specified by the source
Notations
• extend Specifies that the target use case
extends the behaviour of the source.
Actors
Actors
Use Case Diagram – Guidelines
& Caution
• 1.Use cases should ideally begin with a verb – i.e generate report. Use
cases should NOT be open ended – i.e Register (instead should be
named as Register New User)
• 2. Avoid showing communication between actors.
• 3. Actors should be named as singular. i.e student and NOT students.
NO names should be used – i.e John, Sam, etc.
• 4. Do NOT show behaviour in a use case diagram; instead only depict
only system functionality.
• 5. Use case diagram does not show sequence – unlike DFDs
Example – Include and Extend
Draw a use case diagram for the
scenario below:
• Inventory System
• In order to generate an invoice a clerk must log in.
If a clerk is a first time user, one must have
themselves registered. There should be an option
for a user to register oneself within the login page.
Any user can use the system to view products
online. The option of login is also provided when
a user views products online.
Exercise - Solution
Use case diagram
Constructors
• Constructors are special class functions
which performs initialization of every
object.
• The Compiler calls the Constructor
whenever an object is created.
• Constructors initialize values to object
members after storage is allocated to the
object.
Rules of defining a constructor
Example for constructor
Destructors in C++

• Destructor is a special class function which


destroys the object as soon as the scope of
object ends.
• The destructor is called automatically by the
compiler when the object goes out of scope.
Example for destructor
Class Diagram
UML Representation of Class
Relationships among Classes
• Represents a connection between
multiple classes or a class and itself
• 3 basic categories:
– association relationships
– generalization relationships
– aggregation relationships
Association Relationship
• A bidirectional semantic connection between classes
• Name of relationship type shown by:
– drawing line between classes
– labeling with the name of the relationship
– indicating with a small solid triangle beside the name of the relationship
the direction of the association
• Role type shown by:
– drawing line between classes
– indicating with a plus sign before the role
name
Generalization Relationship
• Enables the analyst to create classes that
inherit attributes and operations of other
classes
• Represented by a-kind-of relationship
Generalization Relationship
Generalization Relationship
Aggregation Relationship
• Specialized form of association in which a
whole is related to its part(s)
• Represented by a-part-of relationship
• Denoted by placing a diamond nearest the
class representing the aggregation
Multiplicity
• Documents how many instances of a class
can be associated with one instance of
another class
Multiplicity
• Denotes the minimum number..
maximum number of instances
Exactly one 1
Zero or more 0..* or 0..m
One or more 1..* or 1..m
Zero or one 0..1
Specified range 2..4
Multiple, disjoint ranges 1..3, 5
Guidelines
for Analyzing Use Cases (2)
• An adjective implies an attribute of an
object
• A doing verb implies an operation
• A being verb implies a relationship
between an object and its class
• A having verb implies an aggregation or
association relationship
Guidelines
for Analyzing Use Cases (3)
• A transitive verb implies an operation
• An intransitive verb implies an exception
• A predicate or descriptive verb phrase
implies an operation
• An adverb implies an attribute of a
relationship or an operation
Class Diagram
• Ensure that the classes are both necessary
and sufficient to solve the underlying
problem
– no missing attributes or methods in each
class
– no extra or unused attributes or methods in
each class
– no missing or extra classes

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy