1. A class defines a new user-defined type by encapsulating data members and member functions. Data members store the attributes of an object, while member functions implement the behaviors.
2. An object is an instance of a class that allocates memory for the class's data members. Objects are declared by specifying the class name followed by an identifier. Member functions manipulate the data members of an object using dot or arrow operators.
3. A class's data members and member functions can be accessed privately, publicly, or protected. Constructors initialize new objects by setting initial values for data members. Destructors release resources when objects are destroyed.
In C, a struct models what a thing has/is (i.e., the data, also called the characteristics), but not what it does (its behavior, represented by functions).
The functions are outside and separate from structs.
In C++, the characteristics and behavior are integrated into a single structure, called object.
The data type of an object is the class of the object
The packaging of the data and the functions into a class type is called data encapsulation.
Presentation with a brief history of C, C++ and their ancestors along with an introduction to latest version C++11 and futures such as C++17. The presentation covers applications that use C++, C++11 compilers such as LLVM/Clang, some of the new language features in C++11 and C++17 and examples of modern idioms such as the new form compressions, initializer lists, lambdas, compile time type identification, improved memory management and improved standard library (threads, math, random, chrono, etc). (less == more) || (more == more)
In C, a struct models what a thing has/is (i.e., the data, also called the characteristics), but not what it does (its behavior, represented by functions).
The functions are outside and separate from structs.
In C++, the characteristics and behavior are integrated into a single structure, called object.
The data type of an object is the class of the object
The packaging of the data and the functions into a class type is called data encapsulation.
Presentation with a brief history of C, C++ and their ancestors along with an introduction to latest version C++11 and futures such as C++17. The presentation covers applications that use C++, C++11 compilers such as LLVM/Clang, some of the new language features in C++11 and C++17 and examples of modern idioms such as the new form compressions, initializer lists, lambdas, compile time type identification, improved memory management and improved standard library (threads, math, random, chrono, etc). (less == more) || (more == more)
Slides from my "Gentle Introduction to Modern C++" presentation from January 20, 2015 at the Dublin C/C++ User Group: www.meetup.com/cppdug/events/219787667/
The code examples are located here: https://github.com/mihaitodor/Presentations/tree/master/cppdug/20.01.2015
This set of slides introduces the reader to a subset of the C++ Standard Library called the Standard Template Library (STL). The STL provides a collection of parameterized containers and algorithms, and it is the most successful example of an approach to programming called generic programming. In this presentation, we aim at studying the ideals and concepts of the STL by re-implementing small parts of the library. Specifically, we first show how we can discover requirements on types in order to devise generic algorithms. Then, we focus on how to make algorithms independent of containers through the pivotal abstraction of iterators. To this end, we replicate the standard algorithm for finding the minimum in a sequence (min_element), which we subsequently match with a custom forward iterator over intrusive linked lists of integers. Finally, we see how function objects can be used to customize containers and algorithms alike. This allows us to deepen our understanding of ordering relations, and, in particular, to introduce the concept of strict weak orderings.
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
In this chapter we will get acquainted with the operators in Java and the actions they can perform on the different data types. We will explain the priorities of the operators and we will analyze the different types of operators, according to the count of the arguments they can take and the actions they can perform. In the second part we will examine the conversion of data types.
Stuck with your Ruby Programming Assignment. Get 24/7 help from tutors with Phd in the subject. Email us at support@helpwithassignment.com
Reach us at http://www.HelpWithAssignment.com
Contents :
Language Concepts
How Objective C works- Basics
Data Types
NSInteger
NSNumber
Operators
Loop
Inheritance
Method Overloading
Mutable and Immutable Strings
Mutable and Immutable Arrays
File Management
Slides from my "Gentle Introduction to Modern C++" presentation from January 20, 2015 at the Dublin C/C++ User Group: www.meetup.com/cppdug/events/219787667/
The code examples are located here: https://github.com/mihaitodor/Presentations/tree/master/cppdug/20.01.2015
This set of slides introduces the reader to a subset of the C++ Standard Library called the Standard Template Library (STL). The STL provides a collection of parameterized containers and algorithms, and it is the most successful example of an approach to programming called generic programming. In this presentation, we aim at studying the ideals and concepts of the STL by re-implementing small parts of the library. Specifically, we first show how we can discover requirements on types in order to devise generic algorithms. Then, we focus on how to make algorithms independent of containers through the pivotal abstraction of iterators. To this end, we replicate the standard algorithm for finding the minimum in a sequence (min_element), which we subsequently match with a custom forward iterator over intrusive linked lists of integers. Finally, we see how function objects can be used to customize containers and algorithms alike. This allows us to deepen our understanding of ordering relations, and, in particular, to introduce the concept of strict weak orderings.
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
In this chapter we will get acquainted with the operators in Java and the actions they can perform on the different data types. We will explain the priorities of the operators and we will analyze the different types of operators, according to the count of the arguments they can take and the actions they can perform. In the second part we will examine the conversion of data types.
Stuck with your Ruby Programming Assignment. Get 24/7 help from tutors with Phd in the subject. Email us at support@helpwithassignment.com
Reach us at http://www.HelpWithAssignment.com
Contents :
Language Concepts
How Objective C works- Basics
Data Types
NSInteger
NSNumber
Operators
Loop
Inheritance
Method Overloading
Mutable and Immutable Strings
Mutable and Immutable Arrays
File Management
Pointer
Features of Pointers
Pointer Declaration
Pointer to Class
Pointer Object
The this Pointer
Pointer to Derived Classes and Base Class
Binding Polymorphisms and Virtual Functions
Introduction
Binding in C++
Virtual Functions
Rules for Virtual Function
Virtual Destructor
Defining Simple Classes
Using Own Classes and Objects
Access Modifiers
Constructors and Initializers
Defining Fields
Defining Properties, Getters and Setters
Defining Methods
Exercises: Defining and Using Own Classes
We have compiled the most important slides from each speaker's presentation. This year’s compilation, available for free, captures the key insights and contributions shared during the DfMAy 2024 conference.
A review on techniques and modelling methodologies used for checking electrom...nooriasukmaningtyas
The proper function of the integrated circuit (IC) in an inhibiting electromagnetic environment has always been a serious concern throughout the decades of revolution in the world of electronics, from disjunct devices to today’s integrated circuit technology, where billions of transistors are combined on a single chip. The automotive industry and smart vehicles in particular, are confronting design issues such as being prone to electromagnetic interference (EMI). Electronic control devices calculate incorrect outputs because of EMI and sensors give misleading values which can prove fatal in case of automotives. In this paper, the authors have non exhaustively tried to review research work concerned with the investigation of EMI in ICs and prediction of this EMI using various modelling methodologies and measurement setups.
Hierarchical Digital Twin of a Naval Power SystemKerry Sado
A hierarchical digital twin of a Naval DC power system has been developed and experimentally verified. Similar to other state-of-the-art digital twins, this technology creates a digital replica of the physical system executed in real-time or faster, which can modify hardware controls. However, its advantage stems from distributing computational efforts by utilizing a hierarchical structure composed of lower-level digital twin blocks and a higher-level system digital twin. Each digital twin block is associated with a physical subsystem of the hardware and communicates with a singular system digital twin, which creates a system-level response. By extracting information from each level of the hierarchy, power system controls of the hardware were reconfigured autonomously. This hierarchical digital twin development offers several advantages over other digital twins, particularly in the field of naval power systems. The hierarchical structure allows for greater computational efficiency and scalability while the ability to autonomously reconfigure hardware controls offers increased flexibility and responsiveness. The hierarchical decomposition and models utilized were well aligned with the physical twin, as indicated by the maximum deviations between the developed digital twin hierarchy and the hardware.
4. 4
Class
• The class is the cornerstone of C++
– It makes possible encapsulation, data hiding and inheritance
• Type
– Concrete representation of a concept
• Eg. float with operations like -, *, + (math real numbers)
• Class
– A user defined type
– Consists of both data and methods
– Defines properties and behavior of that type
• Advantages
– Types matching program concepts
• Game Program (Explosion type)
– Concise program
– Code analysis easy
– Compiler can detect illegal uses of types
• Data Abstraction
– Separate the implementation details from its essential properties
5. 5
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Classes & Objects
Rectangle r1;
Rectangle r2;
Rectangle r3;
……
int a;
Objects: Instance of a class
6. 6
Define a Class Type
class class_name
{
permission_label:
member;
permission_label:
member;
...
};
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Body
Header
7. 7
Class Definition
Data Members
• Can be of any type, built-in or user-defined
• non-static data member
– Each class object has its own copy
• static data member
– Acts as a global variable
– One copy per class type, e.g. counter
8. 8
class Rectangle
{
private:
int width;
int length;
static int count;
public:
void set(int w, int
l);
int area();
}
Static Data Member
Rectangle r1;
Rectangle r2;
Rectangle r3;
width
length
width
length
width
length
r1
r3
r2
count
9. 9
Class Definition
Member Functions
• Used to
– access the values of the data members (accessor)
– perform operations on the data members
(implementor)
• Are declared inside the class body
• Their definition can be placed inside the class
body, or outside the class body
• Can access both public and private members of
the class
• Can be referred to using dot or arrow member
access operator
10. 10
Define a Member Function
class Rectangle
{
private:
int width, length;
public:
void set (int w, int l);
int area() {return width*length; }
};
void Rectangle :: set (int w, int l)
{
width = w;
length = l;
}
inline
class name
member function name
scope operator
r1.set(5,8);
rp->set(8,10);
11. 11
• const member function
– declaration
• return_type func_name (para_list) const;
– definition
• return_type func_name (para_list) const { … }
• return_type class_name :: func_name (para_list) const { … }
– Makes no modification about the data members (safe
function)
– It is illegal for a const member function to modify a
class data member
Class Definition
Member Functions
12. 12
Const Member Function
class Time
{
private :
int hrs, mins, secs ;
public :
void Write ( )
const ;
} ;
void Time :: Write( ) const
{
cout <<hrs << “:” << mins << “:” << secs << endl;
}
function declaration
function definition
13. 13
• Information hiding
– To prevent the internal representation from direct
access from outside the class
• Access Specifiers
– public
• may be accessible from anywhere within a program
– private
• may be accessed only by the member functions, and friends
of this class
– protected
• acts as public for derived classes
• behaves as private for the rest of the program
Class Definition - Access Control
14. 14
class Time Specification
class Time
{
public :
void Set ( int hours , int minutes , int seconds ) ;
void Increment ( ) ;
void Write ( ) const ;
Time ( int initHrs, int initMins, int initSecs ) ; // constructor
Time ( ) ; // default constructor
private :
int hrs ;
int mins ;
int secs ;
} ;
14
16. 16
• The default access specifier is private
• The data members are usually private or protected
• A private member function is a helper, may only be
accessed by another member function of the same
class (exception friend function)
• The public member functions are part of the class
interface
• Each access control section is optional, repeatable,
and sections may occur in any order
Class Definition
Access Control
17. 17
What is an object?
OBJECT
Operations
Data
set of methods
(member functions)
internal state
(values of private data members)
18. 18
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Declaration of an Object
main()
{
Rectangle r1;
Rectangle r2;
r1.set(5, 8);
cout<<r1.area()<<endl;
r2.set(8,10);
cout<<r2.area()<<endl;
}
19. 19
Another Example
#include <iostream.h>
class circle
{
private:
double radius;
public:
void store(double);
double area(void);
void display(void);
};
// member function definitions
void circle::store(double r)
{
radius = r;
}
double circle::area(void)
{
return 3.14*radius*radius;
}
void circle::display(void)
{
cout << “r = “ << radius << endl;
}
int main(void) {
circle c; // an object of circle class
c.store(5.0);
cout << "The area of circle c is " << c.area() << endl;
c.display();
}
20. 20
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Declaration of an Object
main()
{
Rectangle r1;
r1.set(5, 8);
}
r1 is statically allocated
width
length
r1
width = 5
length = 8
21. 21
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Declaration of an Object
main()
{
Rectangle r1;
r1.set(5, 8);
Rectangle *r2;
r2 = &r1;
r2->set(8,10);
}
r2 is a pointer to a Rectangle object
width
length
r1
width = 5
length = 8
5000
???
r2
6000
5000
width = 8
length =
10
//dot notation
//arrow notation
22. 22
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Declaration of an Object
main()
{
Rectangle *r3;
r3 = new Rectangle();
r3->set(80,100);
delete r3;
r3 = NULL;
}
r3 is dynamically allocated
???
r3
6000
width
length
5000
5000
width = 80
length = 100
???NULL
//arrow notation
23. 23
#include <iostream.h>
class circle
{
public:
double radius;
};
Object Initialization
int main()
{
circle c1; // Declare an instance of the class circle
c1.radius = 5; // Initialize by assignment
}
1. By Assignment
• Only work for public data
members
• No control over the operations
on data members
24. 24
#include <iostream.h>
class circle
{
private:
double radius;
public:
void set (double r)
{radius = r;}
double get_r ()
{return radius;}
};
int main(void) {
circle c; // an object of circle class
c.set(5.0); // initialize an object with a public member function
cout << "The radius of circle c is " << c.get_r() << endl;
// access a private data member with an accessor
}
Object Initialization
2. By Public Member Functions
25. 25
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
}
Declaration of an Object
main()
{
Rectangle r1;
r1.set(5, 8);
Rectangle *r2;
r2 = &r1;
r2->set(8,10);
}
r2 is a pointer to a Rectangle object
//dot notation
//arrow notation
r1 and r2 are both initialized by
public member function set
26. 26
class Rectangle
{
private:
int width;
int length;
public:
Rectangle();
Rectangle(const Rectangle
&r);
Rectangle(int w, int l);
void set(int w, int l);
int area();
}
Object Initialization
3. By Constructor
• Default constructor
• Copy constructor
• Constructor with parameters
There is no return type
Are used to initialize class data
members
Have the same name as the class
They are publicly accessible
They have different signatures
27. 27
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int l);
int area();
};
Object Initialization
• Default constructor
When a class is declared with no constructors,
the compiler automatically assumes default
constructor and copy constructor for it.
Rectangle :: Rectangle() { };
• Copy constructor
Rectangle :: Rectangle (const
Rectangle & r)
{
width = r.width; length = r.length;
};
28. 28
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int l);
int area();
}
Object Initialization
• Initialize with default constructor
Rectangle r1;
Rectangle *r3 = new Rectangle();
• Initialize with copy constructor
Rectangle r4;
r4.set(60,80);
Rectangle r5 = r4;
Rectangle r6(r4);
Rectangle *r7 = new Rectangle(r4);
29. 29
class Rectangle
{
private:
int width;
int length;
public:
Rectangle(int w, int l)
{width =w; length=l;}
void set(int w, int l);
int area();
}
Object Initialization
If any constructor with any number of
parameters is declared, no default
constructor will exist, unless you
define it.
Rectangle r4; // error
• Initialize with constructor
Rectangle r5(60,80);
Rectangle *r6 = new Rectangle(60,80);
30. 30
class Rectangle
{
private:
int width;
int length;
public:
Rectangle();
Rectangle(int w, int l);
void set(int w, int l);
int area();
}
Object Initialization
Write your own constructors
Rectangle :: Rectangle()
{
width = 20;
length = 50;
};
Rectangle *r7 = new Rectangle();
width
length
width = 20
length = 50
5000???
r7
6000
5000
31. 31
class Account
{
private:
char *name;
double balance;
unsigned int id;
public:
Account();
Account(const Account
&a);
Account(const char
*person);
}
Object Initialization
With constructors, we have more
control over the data members
Account :: Account()
{
name = NULL; balance = 0.0;
id = 0;
};
Account :: Account(const Account &a)
{
name = new char[strlen(a.name)+1];
strcpy (name, a.name);
balance = a.balance;
id = a.id;
};
Account :: Account(const char *person)
{
name = new char[strlen(person)+1];
strcpy (name, person);
balance = 0.0;
id = 0;
};
32. 32
So far, …
• An object can be initialized by a class
constructor
– default constructor
– copy constructor
– constructor with parameters
• Resources are allocated when an object is
initialized
• Resources should be revoked when an
object is about to end its lifetime
33. 33
Cleanup of An Object
class Account
{
private:
char *name;
double balance;
unsigned int id; //unique
public:
Account();
Account(const Account
&a);
Account(const char
*person);
~Account();
}
Destructor
Account :: ~Account()
{
delete[] name;
}
• Its name is the class name
preceded by a ~ (tilde)
• It has no argument
• It is used to release dynamically
allocated memory and to perform
other "cleanup" activities
• It is executed automatically when
the object goes out of scope
34. 34
Putting Them Together
class Str
{
char *pData;
int nLength;
public:
//constructors
Str();
Str(char *s);
Str(const Str &str);
//accessors
char* get_Data();
int get_Len();
//destructor
~Str();
};
Str :: Str() {
pData = new char[1];
*pData = ‘0’;
nLength = 0;
};
Str :: Str(const Str &str) {
int n = str.nLength;
pData = new char[n+1];
nLength = n;
strcpy(pData,str.pData);
};
Str :: Str(char *s) {
pData = new
char[strlen(s)+1];
strcpy(pData, s);
nLength = strlen(s);
};
35. 35
Putting Them Together
class Str
{
char *pData;
int nLength;
public:
//constructors
Str();
Str(char *s);
Str(const Str &str);
//accessors
char* get_Data();
int get_Len();
//destructor
~Str();
};
char* Str :: get_Data()
{
return pData;
};
Str :: ~Str()
{
delete[] pData;
};
int Str :: get_Len()
{
return nLength;
};
36. 36
Putting Them Together
class Str
{
char *pData;
int nLength;
public:
//constructors
Str();
Str(char *s);
Str(const Str &str);
//accessors
char* get_Data();
int get_Len();
//destructor
~Str();
};
int main()
{
int x=3;
Str *pStr1 = new Str(“Joe”);
Str *pStr2 = new Str();
}
38. 38
Working with Multiple Files
• To improve the readability, maintainability and
reusability, codes are organized into modules.
• When working with complicated codes,
– A set of .cpp and .h files for each class groups
• .h file contains the prototype of the class
• .cpp contains the definition/implementation of the class
– A .cpp file containing main() function, should include
all the corresponding .h files where the functions used
in .cpp file are defined
39. 39
Example : time.h
// SPECIFICATION FILE ( time .h )
// Specifies the data members and
// member functions prototypes.
#ifndef _TIME_H
#define _TIME_H
class Time
{
public:
. . .
private:
. . .
} ;
#endif
40. 40
// IMPLEMENTATION FILE ( time.cpp )
// Implements the member functions of class Time
#include <iostream.h>
#include “ time.h” // also must appear in client code
… …
bool Time :: Equal ( Time otherTime ) const
// Function value == true, if this time equals otherTime
// == false , otherwise
{
return ( (hrs == otherTime.hrs) && (mins == otherTime.mins)
&& (secs == otherTime.secs) ) ;
}
. . .
Example : time.cpp
41. 41
Example : main.cpp
// Client Code ( main.cpp )
#include “ time.h”
// other functions, if any
int main()
{
… …
}
Compile and Run
g++ -o mainExec main.cpp time.cpp
42. 42
Separate Compilation and Linking of Files
time.h
main.cpp time.cpp
main.o
mainExec
time.o
Compiler Compiler
Linker
#include “time.h”
implementation file
specification file
main program